使用 LINQ 查找集合中的最新项目,该集合又包含一个集合

发布于 2024-10-23 21:56:12 字数 894 浏览 2 评论 0原文

我有一个产品集合,每个产品都有一个仓库集合,然后在仓库集合内有一个位置集合。

我需要退回所有我做得很好的产品,但我需要返回 locationWithStock,它是仓库集合内位置集合的成员。

到目前为止我已经有了这个但这是错误的,因为 StockDate 不是 Warehouse 的成员而是其集合的成员。我只需要 StockDateS 的最新(最近日期),因此我需要查看每个StockDate,每个成员Location有1个,只返回最新的。

        var items = from j in products
              let locationWithStock =
                            j.Warehouses.OrderByDescending
                         (a => a.StockDate).FirstOrDefault()
                        select new 
                        {
                            Name = j.Name,
                            Warehouse = locationWithStock.Name
                        }

编辑

我会尝试解释更多。这是层次结构:

产品。

每个产品都有多个仓库。

每个仓库都有多个地点。

我需要退回所有产品,并且每个产品都必须包含位置名称。

当有多个仓库、多个地点时,找地点的标准是什么?

我必须在每个仓库和每个位置中进行搜索,并返回所有位置中我使用 StockDate 检测到的最新位置(只有 1 个)。

I have a collection of products, each product has a collection of Warehouses and then inside the collection of warehouses there is a collection of locations.

I need to return all products which I am doing fine but I need to return the locationWithStock which is a member of the collection of locations inside the collection of warehouses.

I have this so far but it's wrong, because StockDate is not a member of Warehouse but of its collection. I need only the Latest (most recent date) of the StockDateS, hence I need to look inside each StockDate which there is 1 in each memeber Location and only return the latest.

        var items = from j in products
              let locationWithStock =
                            j.Warehouses.OrderByDescending
                         (a => a.StockDate).FirstOrDefault()
                        select new 
                        {
                            Name = j.Name,
                            Warehouse = locationWithStock.Name
                        }

Edit

I will try and explain a bit more. Here is the hierarchy:

Products.

Each product has multiple warehouses.

Each warehouse has multiple locations.

I need to return all products and each product must contain the location name.

What is the criteria to find the location when there are multiple warehouses and multiple locations?

I must search in each warehouse and in turn each location, and return OUT OF ALL OF THEM the latest location (ONLY 1) which I detect using the StockDate.

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

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

发布评论

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

评论(1

一生独一 2024-10-30 21:56:12

产品

每个product.Warehouses多个仓库

每个 Warehouse.Locations 每个仓库中有多个位置。

我假设无论返回我做得很好的所有产品(在你的问题中)都会转化为产品状态的某些条件,所以我用 where p.Doing == "Fine"< 表示该条件/code> 下面代码中的 where 子句,您可以适当修改。

var stocks =
           from p in products
           from w in p.Warehouses
           from l in w.locations
           group l by l.Date into g
           let maxDate = g.Max(l1 => l1.Date)
           select new { Product=p.Name, Location= g.Where(l2 => l2.Date == maxDate) };

Products

each product.Warehouses multiple warehouses

each Warehouse.Locations multiple locations in each Warehouse.

I assumed Whatever return all products which i am doing fine(in your question) translates to some condition on prodduct state, so I represented that condition with where p.Doing == "Fine" where clause in below code which you could modify appropriately.

var stocks =
           from p in products
           from w in p.Warehouses
           from l in w.locations
           group l by l.Date into g
           let maxDate = g.Max(l1 => l1.Date)
           select new { Product=p.Name, Location= g.Where(l2 => l2.Date == maxDate) };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文