LINQ:查询集合中的集合
我有以下结构:
public class Customer
{
public int ID { get; set; }
public List<Order> Orders { get; set; }
}
public class Order
{
public int ID { get; set; }
public int ProductID { get set; }
}
我需要获取订购 ProductID = 6 的客户的集合。流畅风格的 LINQ 会是什么样子?
我尝试了下面但没有运气:
var customers = allCustomers.SelectMany(c => c.Orders.Select(o => o.ProductID.Equals(6))).ToArray();
I have following structure:
public class Customer
{
public int ID { get; set; }
public List<Order> Orders { get; set; }
}
public class Order
{
public int ID { get; set; }
public int ProductID { get set; }
}
I need to get the collection of customers that ordered ProductID = 6. What would the fluent style LINQ look like?
I tried below with no luck:
var customers = allCustomers.SelectMany(c => c.Orders.Select(o => o.ProductID.Equals(6))).ToArray();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
varcustomers = allCustomers.Where(c => c.Orders.Any(o => o.ProductID == 6));
var customers = allCustomers.Where(c => c.Orders.Any(o => o.ProductID == 6));
看起来你想要:
Looks like you want:
一个很好的方法是从另一边尝试像这样
当然你需要在 Order 类中添加 Customer 属性,如下所示
A nice way would be to try from the other side like this
And of course you need the add the Customer property in the Order class like this