Dynamic Projection in linq with List of fieldNames

c# dynamic-linq entity-framework entity-framework-6 linq

Question

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

1
0
7/10/2018 8:54:14 AM

Accepted Answer

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

1
7/11/2018 10:25:53 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow