Search code examples
c#lambdadynamic-linq

Dynamic LINQ - select certain fields from ICollection Field


I have table like this:

class x
{
public int Id {set;get;}
public ICollection<y> items {set;get;}
}
class y
{
public int Id {set;get;}
public string name {set;get;}
public int price {set;get;}
public datetime create_date {set;get;}
public int count {set;get;}
}

and I want to query dynamically to get all items y (just name and price fields) that meets the condition x.id > 100

I use Dynamic-LINQ

everything is fine with the where condition, my issue is in select statement, I've tried the following:

db.x.where("id>100").select("new (items.select("new {name,price}"))");

and

db.x.where("id>100").select("new (items.select("name,price")");

nothing is working!

could you please help me!

I've tried to return all fields, and it's working fine, like this:

db.x.where("id>100").select("new (items)");

Solution

  • Online example of Dynamic LINQ - SelectMany

    You can try using these samples:
    db.x.Where("Id > 100").SelectMany("x => x.items.Select(r => new (r.name, r.price))")
    or
    db.x.Where("Id > 100").SelectMany("items").Select("new (name, price)")

    Code example on dotnetfiddle