Apply Group by,然后选择all
本文关键字:选择 all 然后 Group by Apply | 更新日期: 2023-09-27 18:08:17
我有一个包含以下项目的列表:
Id
名称
货币
基金
我在这个列表中应用组:
var testData = from fl in fundsList
group fl by new { fl.Name } into groupedData
select new
{
groupedData.Key.Name,
};
但是这只给了我选择数据中的Name。我想要的是基于名称应用组,并获得所有项目,即Id,名称,货币和基金在选择。我试着在select中访问其他的,但它们没有Key。
groupedData
在查询中使用属于该组的项实现IEnumerable<YourEntity>
,因此您可以轻松地执行以下操作:
var testData = from fl in fundsList
group fl by fl.Name into groupedData
select new
{
Name = groupedData.Key.Name,
Items = grp.ToList()
};
或者在基于方法的查询中:
var testData = fundsList.GroupBy(fl => fl.Name)
.Select(g => new { Name = g.Key, Items = g.ToList() });
testData
集合中的每个项目将有两个属性:
- 组名
-
Items
包含所有属于 组的项目
Name
试试这个:
var testData = fundsList.GroupBy(fl => fl.Name);
现在您将得到一个IEnumerable<IGrouping<T>>
,其中包含许多key => valueCollection项。
var testData = from fl in fundsList
group fl by new { fl.Name } into groupedData
select new
{
GroupName= groupedData.Key.Name,
Items = grp.ToList()
};