How to create LINQ Expression Tree to select an anonymous type

前端 未结 9 731
夕颜
夕颜 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:57

    You could use a parameter class instead of working with an anonymous type. In your example you can create a parameter class like this:

    public struct ParamClass {
        public string Name { get; set; };
        public int Population { get; set; };
    }
    

    …and put it into your select like this:

    var v = from c in Countries
            where c.City == "London"
            select new ParamClass {c.Name, c.Population};
    

    What you get out is something of the type IQueryable.

提交回复
热议问题