Complex Dynamic Search

2 posts, 0 answers
  1. Ujjwal Manna
    Ujjwal Manna avatar
    8 posts
    Member since:
    Dec 2009

    Posted 15 Dec 2009 Link to this post

    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



  2. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 17 Dec 2009 Link to this post

    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.
  3. DevCraft banner
Back to Top