If I wanted to return just the ID, and Name from a collection of Customer entities, what would be the recommended way of doing this? Should I use the Entity Set and then pick off what I need as I loop the collection? Is there LINQ syntax that can give me the equivalent of a SQL Select clause (i.e. Select ID, Name From Customer).
Thank you,
Stephen
Use Queryable.Select
<TSource, TResult> to project the collection:Note that will project the results into a
List<T>of anonymous types.If you want to project it into something else (e.g custom class/POCO), you’ll need to materalize the result set first, and then project the query (e.g
.ToList()and then.Select()).In other words, if you do this:
You will get an EF error (cannot be translated to Linq-Entities query – as “CutDownCustomer” is not part of the conceptual model).
So you must do this:
Of course, if you only require method scope for your result set, anonymous types should be sufficient.
HTH.