如何使用 LINQ 对集合中的项目进行分组并根据集合类型返回整形数据
我有以下集合
public IQueryable<myObjectType > GetWorkCellLoadGraphDataByIdDummy()
{
IList<myObjectType> workCellLoadGraphDataCollection = new List<myObject>()
{
new myObjectType(DateTime.Today.AddHours(8).AddMinutes(30), 1),
new myObjectType(DateTime.Today.AddHours(10).AddMinutes( 10 ), 6 ),
new myObjectType(DateTime.Today.AddHours(13).AddMinutes( 30 ),8 ),
new myObjectType(DateTime.Today.AddDays(1).AddHours(8).AddMinutes(30), 1),
new myObjectType(DateTime.Today.AddDays(1).AddHours( 10 ).AddMinutes( 10 ), 5 ),
new myObjectType(DateTime.Today.AddDays(1).AddHours( 13 ).AddMinutes( 30 ), 2 )
};
// Write some LINQ code to group data according to first parameter
// Perform sum of last parameter
// Shape the data to be in the form of myObjectType
// return result;
}
,我想要做的是按 myObjectType 类的第一个参数对项目进行分组。
然后对于每个分组,我想对所有最后一个参数进行求和。
最后,结果应该以“myObjectType”的形式返回,
我知道如何用老式的方式来做,即循环遍历所有项目并进行求和。然而,我想学习如何在 LINQ 中做到这一点,这是我刚刚开始的。
谁能给我指出正确的方向,以便我可以将我的需求转换为 LINQ?
实际上,结果应该是一个包含两个 myObjectType 类型对象的集合,如下所示:
- 集合中的第一个对象是 (DateTime.Today, 15 )
- 集合中的第二个对象是 (DateTime.Today.AddDays(1), 8)
TIA,
David
I have the following collection
public IQueryable<myObjectType > GetWorkCellLoadGraphDataByIdDummy()
{
IList<myObjectType> workCellLoadGraphDataCollection = new List<myObject>()
{
new myObjectType(DateTime.Today.AddHours(8).AddMinutes(30), 1),
new myObjectType(DateTime.Today.AddHours(10).AddMinutes( 10 ), 6 ),
new myObjectType(DateTime.Today.AddHours(13).AddMinutes( 30 ),8 ),
new myObjectType(DateTime.Today.AddDays(1).AddHours(8).AddMinutes(30), 1),
new myObjectType(DateTime.Today.AddDays(1).AddHours( 10 ).AddMinutes( 10 ), 5 ),
new myObjectType(DateTime.Today.AddDays(1).AddHours( 13 ).AddMinutes( 30 ), 2 )
};
// Write some LINQ code to group data according to first parameter
// Perform sum of last parameter
// Shape the data to be in the form of myObjectType
// return result;
}
and what I want to do is to group items by the first parameter of the myObjectType class.
Then for each grouping, I'd like to do the sum of all the last parameters.
Finally the result should be returned in the form of "myObjectType"
I know how to do it the old fashioned way i.e. looping through all the items and doing the sums. However, I'd like to learn how to do it in LINQ which is something I've just started.
Can anyone point me in the right direction so that I can translate my requirements into LINQ?
In effect, the result should be a collection containing two objects of type myObjectType as follows:
- First object in collection is (DateTime.Today, 15 )
- Second object in collection is (DateTime.Today.AddDays(1), 8)
TIA,
David
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
给定一个具有这种基本设计的类,
您可以通过以下方式使用 LINQ 来满足您的要求。第一个示例使用流畅的扩展方法语法生成
IEnumerable
。第二个版本实现了相同的结果,但使用了更像 SQL 的查询表达式语法。
从那里,您可以利用扩展方法
AsQueryable
生成IQueryable
结果,或ToList()
/ToArray()
产生具体的集合。Given a class with this basic design
You could fulfill your requirement using LINQ in the manner below. The first example produces
IEnumerable<MyObjectType>
using fluent extension method syntax.The second version achieves the same result but uses the more SQL-esque query expression syntax.
From there, you utilize the extension methods
AsQueryable
to yield anIQueryable
result, orToList()
/ToArray()
to yield concrete collections.