0

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)");
  • related : [How to convert a String to its equivalent LINQ Expression Tree?](https://stackoverflow.com/questions/821365/) – Drag and Drop Mar 03 '21 at 07:04

2 Answers2

1

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

Badj
  • 71
  • 5
  • do you know if I can select extra fields from class x, etc.. id ? is selectmany does the work ? – Mohammed Alzamil Mar 03 '21 at 10:34
  • Try to using `db.x.Where("Id > 10").SelectMany("x => x.items.Select(r => new (x.Id as ParentId, r.name, r.price))");` to get `x.Id` as `ParentId` in result collection – Badj Mar 03 '21 at 11:41
0

Try using adding one more select after selecting all the items

db.x.where("id>100").select("new (items)").select("new {name,price}");
Ram Prasad
  • 23
  • 1
  • 5
  • I tried your solution, unfortunately, is not working, anyway, I can get fields like this (it's not fit me): Select("new (items[0].name , items[0].price)") Coz this way i can get one item each time, I need them all – Mohammed Alzamil Mar 03 '21 at 08:17