C# Linq where clause as a variable

后端 未结 5 741
后悔当初
后悔当初 2020-12-01 07:30

I am trying to make a LINQ statement where the where clause comes from a variable. For example:

string whereClause = address.zip == 23456;
var x = from somet         


        
5条回答
  •  被撕碎了的回忆
    2020-12-01 08:10

    This:

    var query = from something in someList where whereClause;
    

    is shorthand for:

    var query = someList.Where(something => whereClause);
    

    Assuming someList is an IEnumerable

    , Where refers to the Enumerable.Where Extension Method. This method expects a Func which you can define as follows:

    Func whereClause = address => address.Zip == 23456;
    var query = someList.Where(whereClause);
    

提交回复
热议问题