Masoud Mirzaei
Masoud Mirzaei

Reputation: 51

Dynamic Projection in linq with List of fieldNames

I wanna do projection on fields of my collection and nested collection in each of entity, I use EntityFramework 6.2 I've do that with dynamic query like bellow code

Students.Select("new (Name,Family,new(Category.Name) as Category)");

it was worked but when I want to do that on the collection it's thrown error

  Students.Select("new (Name,Family,new(Courses.Name,Courses.UnitName) as Courses)");

I want,it returns result as bellow code returns

Students.Select(std=>new{
 std.Name, 
 Category=new{std.Category.Name},
 Courses=std.Courses.Select(co=>new{
  co.Name,co.UnitName
})}) ;

please if you have any idea share with me

Upvotes: 0

Views: 1330

Answers (1)

xanatos
xanatos

Reputation: 111920

You can't do it with System.Linq.Dynamic. It is possible with System.Linq.Dynamic.Core (a more advanced fork of the library).

Equivalent to the query you wrote in non-dynamic way:

var q = Students.Select("new (Name, Category.Name as Category, Courses.Select(new (Name, UnitName)) as Courses)");

What you were looking for is simply Courses.Select(new (field1, field2)) as SomeAlias

Upvotes: 2

Related Questions