This is a migrated thread and some comments may be shown as answers.

Complex Dynamic Search

1 Answer 49 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ujjwal Manna
Top achievements
Rank 1
Ujjwal Manna asked on 15 Dec 2009, 04:12 PM
Hello Guys,

I am stuck in a issue.It would be great if  you can help on this. The problem like below

My class object contain 3 fields(Fname,Lname,Email). 

Now I have to write a  function which contain two parameters ( string [] SearchText,string [] SearchFieldListName) and I have to return the records which make sure that combination of search field must contain the search text.  

For ex : My records contain the  record like below

Fname   Lname     Email
Ujjwal     Manna    ujm@abc.com

For the above record  the input parameter of the above function will be like  ( new []{ "Uj ","anna"},new []{"Fname","Lname"}) 
or  ( new []{ "anna ","Uj"},new []{"Fname","Lname"}) .

It will return the above records because the combination Fname & Lname contain the text "anna" & "Uj".

Could you please tell me what will be the Linq query in Telerik ORM for this. Any suggestion will be appreciated.

Regards,
Ujjwal



1 Answer, 1 is accepted

Sort by
0
Damyan Bogoev
Telerik team
answered on 17 Dec 2009, 04:11 PM
Hi Ujjwal Manna,

You could use the following methods to filter the records:

public static List<string> PropertyValues(YourClass user, List<string> fields)
{
    List<string> result = new List<string>();
    foreach (string field in fields)
    {
        result.Add(typeof(YourClass).GetProperty(field).GetValue(user, null).ToString());
    }
 
    return result;
}
 
public static bool Filter(YourClass user, List<string> fields, List<string> filters)
{
    List<string> propertyValues = PropertyValues(user, fields);
 
    foreach (string filter in filters)
    {
        if (!propertyValues.Any(pv => pv.Contains(filter)))
        {
            return false;
        }
    }
 
    return true;
}
 
public static Func<YourClass, bool> FilterExpression(List<string> fields, List<string> filters)
{
    return (YourClass user) => Filter(user, fields, filters);
}
 
public static IEnumerable<YourClass> FilterUsers(IEnumerable<YourClass> users, List<string> fields, List<string> filters)
{
    for (int i = 0; i < fields.Count; i++)
    {
        users = users.Where(FilterExpression(fields, filters));
    }
    return users;
}
 
 
...
 
List<string> fields1 = new List<string> { "FirstName", "LastName" };
List<string> filters1 = new List<string> { "Uj", "anna" };
 
List<string> fields2 = new List<string> { "FirstName", "LastName" };
List<string> filters2 = new List<string> { "anna", "Uj" };
 
var result1 = FilterUsers(scope.Extent<Users>(), fields1, filters1);
var result2 = FilterUsers(scope.Extent<Users>(), fields2, filters2);
 
...
The idea is that each search string should take place in at least one property. If a search string is not found in the object's properties, the object is not returned.
Hope this is what you want to achieve.

Greetings,
Damyan Bogoev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
LINQ (LINQ specific questions)
Asked by
Ujjwal Manna
Top achievements
Rank 1
Answers by
Damyan Bogoev
Telerik team
Share this question
or