使用 LINQ 查找集合中的最新项目,该集合又包含一个集合
我有一个产品集合,每个产品都有一个仓库集合,然后在仓库集合内有一个位置集合。
我需要退回所有我做得很好的产品,但我需要返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
产品
每个product.Warehouses多个仓库
每个 Warehouse.Locations 每个仓库中有多个位置。
我假设无论返回我做得很好的所有产品(在你的问题中)都会转化为产品状态的某些条件,所以我用
where p.Doing == "Fine"< 表示该条件/code> 下面代码中的 where 子句,您可以适当修改。
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.