How to create predicate dynamically

后端 未结 3 1673
感动是毒
感动是毒 2020-12-17 19:23

Hi i want to create a list based on the search string using predicate expressions.

I have a list of type products contains different names.

List

3条回答
  •  执笔经年
    2020-12-17 20:04

    You don't have to build a predicate here. You can try something like this

    List list1 = new List();
    
    list1.Add(new products("sowmya"));
    list1.Add(new products("Jane"));
    list1.Add(new products("John"));
    list1.Add(new products("kumar"));
    list1.Add(new products("ramya"));
    
    string input = "aaa+kuma+ram";
    List searchStrings =
        input.Split(new string[] { "+" }, StringSplitOptions.None)
        .Select(s => s.ToLower())
        .ToList();
    
    List list2 = (
        from p in list1
        where searchStrings.Any(s => p.Name.Contains(s))
        select p).ToList();
    

    list2 will contain "kumar" and "ramya".

提交回复
热议问题