Dynamic LINQ on IEnumerable?

前端 未结 2 630
野性不改
野性不改 2021-01-01 18:37

Say i need to filter a generic list with a dynamic query (List l; var x = l.Where(*dynamic query*))

How would i do this using LINQ? (Curre

2条回答
  •  爱一瞬间的悲伤
    2021-01-01 19:11

    You can use the FindAll() method which takes a predicate. Here is a basic example.

    List stringList = new List(new string[]{"Smith", "Johnson", "Jordan","Doe"});
    
    List filteredStringList = stringList.FindAll(x => x == "Smith");
    

    Also the Find method returns a single item.

    There is also an example project on MSDN for executing dynamic LINQ queries on both IEnumerable and IQueryable. You can reuse the DynamicQueryable class from it. Here is the link. The project is inside the C# samples project.

提交回复
热议问题