微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

动态 LINQ - 从 ICollection 字段中选择某些字段

如何解决动态 LINQ - 从 ICollection 字段中选择某些字段

我有这样的桌子:

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;}
}

并且我想动态查询获取满足条件 x.id > 100 的所有项目 y(仅名称和价格字段)

我使用 Dynamic-LINQ

where 条件一切正常,我的问题出在 select 语句中,我尝试了以下操作:

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

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

没有任何效果

你能帮我吗!

我尝试返回所有字段,并且工作正常,如下所示:

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

解决方法

Dynamic LINQ - SelectMany 的在线示例

您可以尝试使用以下示例:
db.x.Where("Id > 100").SelectMany("x => x.items.Select(r => new (r.name,r.price))")

db.x.Where("Id > 100").SelectMany("items").Select("new (name,price)")

Code example on dotnetfiddle

,

尝试在选择所有项目后再添加一个选择

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。