[英]LINQ Select a List of List with DISTINCT
imagine to have a class (and corresponding db table) as follows 假设有一个类(和相应的数据库表),如下所示
class Price{
int ItemID;
int ItemTypeID;
string ItemName;
string ItemTypeName;
float Price;
}
I am seraching for a new to query it via LINQ in order to get the list of distinct Items (possibly with the use of Take() and Skip() methods) and nested the list of all associated prices. 我正在寻找一种通过LINQ查询它的新商品,以获取不同商品的列表(可能使用Take()和Skip()方法)并嵌套所有相关价格的列表。
Any suggestion? 有什么建议吗?
EDIT: To make the question simpler here's an example 编辑:为了使问题更简单,这是一个示例
Imagine to have 3 "prices" as following 想象一下,有以下三个“价格”
1, 1, Ball, Blue, 10
1, 2, Ball, Red, 20
2, 1, Frisbee, Blue, 30
I would like to put them in a simplified structure 我想把它们简化
List<Item>
where 哪里
class Item
{
string ItemName;
string ItemTypeName;
List<Price> Prices;
}
and 和
class Price
{
float Price;
}
Use Distinct()
, like this: 使用
Distinct()
,如下所示:
var results = _dbContext.Prices
.Distinct()
.Skip(15)
.Take(5);
Edit: To populate List<Item>
with a list of prices for each item as you asked you should use a GROUPBY
like this: 编辑:按照您的要求为每个商品的价格列表填充
List<Item>
,您应该使用GROUPBY
像这样:
var results = _dbContext.GroupBy( i => new { i.ItemName, i.ItemTypeName })
.Select( g => new Item()
{
ItemName = g.Key.ItemName,
ItemTypeName = g.Key.ItemTypeName,
Prices = g.Select( p => new Price(){Price = p.Price})
});
After that you can applay Take
and Skip
the same way as in the first query. 之后,您可以
Take
与第一个查询相同的方式应用“ Take
和“ Skip
”。
It sounds like maybe you want a GroupBy. 听起来好像您想要一个GroupBy。 Try something like this.
尝试这样的事情。
var result = dbContext.Prices
.GroupBy(p => new {p.ItemName, p.ItemTypeName)
.Select(g => new Item
{
ItemName = g.Key.ItemName,
ItemTypeName = g.Key.ItemTypeName,
Prices = g.Select(p => new Price
{
Price = p.Price
}
).ToList()
})
.Skip(x)
.Take(y)
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.