LINQ 投影帮助

发布于 2024-11-01 02:42:48 字数 789 浏览 0 评论 0 原文

我有一个 ICollection

var products = ProductRepository.FindAll();

Product 有一个名为 Orders 的属性code>,这是一个 ICollection

我试图最终得到给定 CustomerIdICollection

换句话说:

给定一组产品,我想检索特定客户的订单列表

这就是我所拥有的:

var orders = products
               .Where(x => x.Orders != null)
               .Where(x => x.Orders.Any(y => y.CustomerId == 10))
               .Select(x => x.Orders)
               .ToList();

但我最终得到一个 List>,其中我想要一个 <代码>ICollection<订单>。

我必须进行某种分组吗?

I have an an ICollection<Product>:

var products = productRepository.FindAll();

Product has a property called Orders, which is an ICollection<Order>.

I'm trying to end up with an ICollection<Order>, for a given CustomerId.

In other words:

Given a collection of products, i want to retrieve a list of orders for a particular customer

Here's what i have:

var orders = products
               .Where(x => x.Orders != null)
               .Where(x => x.Orders.Any(y => y.CustomerId == 10))
               .Select(x => x.Orders)
               .ToList();

But i end up with an List<ICollection<Order>>, where i want a ICollection<Order>.

Do i have to do some kind of grouping?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怼怹恏 2024-11-08 02:42:48

怎么样:

var orders = products
           .Where(x => x.Orders != null)
           .Where(x => x.Orders.Any(y => y.CustomerId == 10))
           .SelectMany(x => x.Orders)
           .ToList();

What about:

var orders = products
           .Where(x => x.Orders != null)
           .Where(x => x.Orders.Any(y => y.CustomerId == 10))
           .SelectMany(x => x.Orders)
           .ToList();
云归处 2024-11-08 02:42:48

如果您想要收集单个产品的订单,那么这可以工作

var orders = products
               .Where(x => x.Orders != null)
               .Where(x => x.Orders.Any(y => y.CustomerId == 10))
               .Select(x => x.Orders)
               .Single();

If you want the collection of orders for single product then this would work

var orders = products
               .Where(x => x.Orders != null)
               .Where(x => x.Orders.Any(y => y.CustomerId == 10))
               .Select(x => x.Orders)
               .Single();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文