c# - 如何优化 linq 查询以在不合并结果的情况下按价格对日期进行分组

标签 c# linq group-by

我有如下所述的 linq 查询。问题是当数据按价格分组时,它按价格对日期分组,而没有考虑非连续日期可能出现相同价格的情况

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        //Console.WriteLine("Hello World");
        List<Prices> list = new List<Prices>();
        list.Add(new Prices() { Date = DateTime.Parse("2017-06-17"), Price = Double.Parse("50")});
        list.Add(new Prices() { Date = DateTime.Parse("2017-06-18"), Price = Double.Parse("50")});
        list.Add(new Prices() { Date = DateTime.Parse("2017-06-19"), Price = Double.Parse("50")});
        list.Add(new Prices() { Date = DateTime.Parse("2017-06-20"), Price = Double.Parse("100")});
        list.Add(new Prices() { Date = DateTime.Parse("2017-06-21"), Price = Double.Parse("100")});
        list.Add(new Prices() { Date = DateTime.Parse("2017-06-22"), Price = Double.Parse("100")});
        list.Add(new Prices() { Date = DateTime.Parse("2017-06-23"), Price = Double.Parse("50")});
        list.Add(new Prices() { Date = DateTime.Parse("2017-06-24"), Price = Double.Parse("50")});
        list.Add(new Prices() { Date = DateTime.Parse("2017-06-25"), Price = Double.Parse("50")});


        var baseservices = list
                .GroupBy(l => l.Price)
                .Select(g => new
                {
                    Price = g.Key,
                    PeriodStart = g.Select(l=>l.Date).Min(),
                    PeriodEnd = g.Select(l => l.Date).Max(),
                });

        foreach(var item in baseservices)
        {
            Console.WriteLine(item.Price + " " + item.PeriodStart + " " + item.PeriodEnd);  
        }

    }
}

public class Prices
{
    public DateTime Date {get;set;}
    public double Price {get;set;}  
}

public class Quote
{
    public DateTime PeriodStart {get;set;}
    public DateTime PeriodEnd {get;set;}
    public double Price {get;set;}  
}

结果是

50 6/17/2017 12:00:00 AM 6/25/2017 12:00:00 AM
100 6/20/2017 12:00:00 AM 6/22/2017 12:00:00 AM

如何得到下面的结果

50 6/17/2017 12:00:00 AM 6/29/2017 12:00:00 AM
100 6/20/2017 12:00:00 AM 6/22/2017 12:00:00 AM
50 6/23/2017 12:00:00 AM 6/25/2017 12:00:00 AM

最佳答案

LINQ 不太适合此类操作。唯一可用于执行此类处理的标准 LINQ 运算符是 Aggregate ,但它只不过是 LINQ 风格的 foreach 循环:

var baseservices = list
    .OrderBy(e => e.Date)
    .Aggregate(new List<Quote>(), (r, e) =>
    {
        if (r.Count > 0 && r[r.Count - 1].Price == e.Price && r[r.Count - 1].PeriodEnd.AddDays(1) == e.Date)
            r[r.Count - 1].PeriodEnd = e.Date;
        else
            r.Add(new Quote { Price = e.Price, PeriodStart = e.Date, PeriodEnd = e.Date });
        return r;
    });

请注意,与许多 LINQ 方法相比,这会立即执行并且在整个结果准备就绪之前不会返回。

关于c# - 如何优化 linq 查询以在不合并结果的情况下按价格对日期进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41653586/

相关文章:

c# - 将多个资源字典动态添加到我的 WPF 项目

c# - 在作为数组的 ICollection 上调用 .toArray() 是否返回引用或副本?

c# - 使用 LINQ 将类的字段复制到字典列表中

C# 加入列表。 LINQ。无法创建常量类型

c# - IQueryable<T> 给出的结果与 List<T> 不同

python - 比较 Pandas 数据框中一组子组的值

c# - 如何用C#绘制流畅的图像?

c# - 配置 WPF 客户端以运行 64 位

python - 重采样错误 : cannot reindex a non-unique index with a method or limit

MySQL:如何获得每个分组的 x 个结果