如何使用 LINQ 对集合中的项目进行分组并根据集合类型返回整形数据

发布于 2024-10-09 00:04:47 字数 1400 浏览 1 评论 0原文

我有以下集合

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 技术交流群。

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

发布评论

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

评论(1

錯遇了你 2024-10-16 00:04:47

给定一个具有这种基本设计的类,

class MyObjectType
{
    public MyObjectType(DateTime date, int count)
    {
        this.MyDate = date;
        this.MyCount = count;
    }

    public DateTime MyDate { get; set; }
    public int MyCount { get; set; }
}

您可以通过以下方式使用 LINQ 来满足您的要求。第一个示例使用流畅的扩展方法语法生成 IEnumerable

var query = collection.GroupBy(obj => obj.MyDate.Date)
                      .Select(grp =>
                                new MyObjectType(grp.Key, grp.Sum(obj => obj.MyCount))
                             );

第二个版本实现了相同的结果,但使用了更像 SQL 的查询表达式语法。

var query = from obj in collection
            group obj by obj.MyDate.Date into grp
            let mySum = grp.Sum(item => item.MyCount)
            select new MyObjectType(grp.Key, mySum);

从那里,您可以利用扩展方法 AsQueryable 生成 IQueryable 结果,或 ToList() / ToArray() 产生具体的集合。

Given a class with this basic design

class MyObjectType
{
    public MyObjectType(DateTime date, int count)
    {
        this.MyDate = date;
        this.MyCount = count;
    }

    public DateTime MyDate { get; set; }
    public int MyCount { get; set; }
}

You could fulfill your requirement using LINQ in the manner below. The first example produces IEnumerable<MyObjectType> using fluent extension method syntax.

var query = collection.GroupBy(obj => obj.MyDate.Date)
                      .Select(grp =>
                                new MyObjectType(grp.Key, grp.Sum(obj => obj.MyCount))
                             );

The second version achieves the same result but uses the more SQL-esque query expression syntax.

var query = from obj in collection
            group obj by obj.MyDate.Date into grp
            let mySum = grp.Sum(item => item.MyCount)
            select new MyObjectType(grp.Key, mySum);

From there, you utilize the extension methods AsQueryable to yield an IQueryable result, or ToList() / ToArray() to yield concrete collections.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文