How to create LINQ Expression Tree to select an anonymous type

前端 未结 9 732
夕颜
夕颜 2020-11-22 12:12

I would like to generate the following select statement dynamically using expression trees:

var v = from c in Countries
        where c.City == \"London\"
           


        
9条回答
  •  执笔经年
    2020-11-22 12:53

    You could use the IQueryable-Extensions here, which is an implemantation of the solution described by "Ethan J. Brown":

    https://github.com/thiscode/DynamicSelectExtensions

    The Extension builds dynamically an anonymous type.

    Then you can do this:

    var YourDynamicListOfFields = new List(
    
        "field1",
        "field2",
        [...]
    
    )
    var query = query.SelectPartially(YourDynamicListOfFields);
    

提交回复
热议问题