Hi I am trying to use Lambda Expression to cast anonymous type list to primitive type list, so far I have no luck.
I know I can use foreach to loop through ‘a’ and get the values, but I want to know how to do it in Lambda or Linq.
var a = new []{ new {name= "jerry", age = 32}, new {name="peter",age=23}};
List<string> b = a.Select(p => new System.String{p.name}).ToList();
List<int> c = a.Select(p => new System.Int32{p.age}).ToList();
The problem is with your use of { and } in
new System.String{p.name}. That isn’t a valid way to instantiate a type.