按日期分组,其中第二个列表中的日期从第一个日期起算

时间:2019-01-11 12:22:49

标签: c# .net linq

我有两个表:

第一个是 TimeValues (示例)

time     | value
12/28/18 | 5.6
01/03/19 | 5.6
01/04/19 | 5.6
01/09/19 | 5.6
01/15/19 | 5.6
02/03/19 | 5.6

第二个是 LogicalPeriods

DateFrom | DateTo
12/16/18 | 12/23/18
12/23/18 | 12/30/18
12/30/18 | 01/06/19
01/06/19 | 01/13/19
01/13/19 | 01/20/19
01/20/19 | 01/27/19
01/27/19 | 02/03/19
02/03/19 | 02/10/19

我需要使用Linq的 TimeValues 表与 LogicalPeriods 表(时间必须为> DateFrom 和<= DateTo ),并且如果结果之间没有任何时间值,结果表将具有两列 DateTo SUM(value)期间,仅返回null。以我的例子

DateTo   | value
12/23/18 | null
12/30/18 | 5.6
01/06/19 | 11.2
01/13/19 | 5.6
01/20/19 | 5.6
01/27/19 | null
02/03/19 | 5.6
02/10/19 | null

这是到目前为止我所拥有的一个例子

var TimeValues = new List<TimeValue>
{
    new TimeValue {time = DateTime.Parse("12/28/18"), value = 5.6 },
    new TimeValue {time = DateTime.Parse("01/03/19"), value = 5.6 },
    new TimeValue {time = DateTime.Parse("01/04/19"), value = 5.6 },
    new TimeValue {time = DateTime.Parse("01/09/19"), value = 5.6 },
    new TimeValue {time = DateTime.Parse("01/15/19"), value = 5.6 },
    new TimeValue {time = DateTime.Parse("02/03/19"), value = 5.6 },
};

var LogicalPeriods = new List<LogicalPeriod>
{
    new LogicalPeriod { DateFrom = DateTime.Parse("12/28/18"), DateTo = DateTime.Parse("12/23/18") },
    new LogicalPeriod { DateFrom = DateTime.Parse("12/23/18"), DateTo = DateTime.Parse("12/30/18") },
    new LogicalPeriod { DateFrom = DateTime.Parse("12/30/18"), DateTo = DateTime.Parse("01/06/19") },
    new LogicalPeriod { DateFrom = DateTime.Parse("01/06/19"), DateTo = DateTime.Parse("01/13/19") },
    new LogicalPeriod { DateFrom = DateTime.Parse("01/13/19"), DateTo = DateTime.Parse("01/20/19") },
    new LogicalPeriod { DateFrom = DateTime.Parse("01/20/19"), DateTo = DateTime.Parse("01/27/19") },
    new LogicalPeriod { DateFrom = DateTime.Parse("01/27/19"), DateTo = DateTime.Parse("02/03/19") },
    new LogicalPeriod { DateFrom = DateTime.Parse("02/03/19"), DateTo = DateTime.Parse("02/10/19") },
};

var result = LogicalPeriods.GroupJoin(
    TimeValues,
    period => new { period.DateFrom, period.DateTo },
    tv => tv.time,
    (period, tv) => new {period.DateTo, timeValues = tv })// I don't know what should I need do here

我发现GroupJoin仅使用相等比较器,但是我需要之间的比较。将有助于建议使用Method语法的解决方案。

1 个答案:

答案 0 :(得分:3)

使用以下逻辑,我能够获得上面显示的预期结果

CultureInfo provider = CultureInfo.InvariantCulture;
var timeValues = new List<TimeValue>
{
    new TimeValue {time = DateTime.ParseExact("12/28/18","MM/dd/yy",provider), value = 5.6 },
    new TimeValue {time = DateTime.ParseExact("01/03/19","MM/dd/yy",provider), value = 5.6 },
    new TimeValue {time = DateTime.ParseExact("01/04/19","MM/dd/yy",provider), value = 5.6 },
    new TimeValue {time = DateTime.ParseExact("01/09/19","MM/dd/yy",provider), value = 5.6 },
    new TimeValue {time = DateTime.ParseExact("01/15/19","MM/dd/yy",provider), value = 5.6 },
    new TimeValue {time = DateTime.ParseExact("02/03/19","MM/dd/yy",provider), value = 5.6 },
};

var logicalPeriods = new List<LogicalPeriod>
{
    new LogicalPeriod { DateFrom = DateTime.ParseExact("12/28/18","MM/dd/yy",provider), DateTo = DateTime.ParseExact("12/23/18","MM/dd/yy",provider) },
    new LogicalPeriod { DateFrom = DateTime.ParseExact("12/23/18","MM/dd/yy",provider), DateTo = DateTime.ParseExact("12/30/18","MM/dd/yy",provider) },
    new LogicalPeriod { DateFrom = DateTime.ParseExact("12/30/18","MM/dd/yy",provider), DateTo = DateTime.ParseExact("01/06/19","MM/dd/yy",provider) },
    new LogicalPeriod { DateFrom = DateTime.ParseExact("01/06/19","MM/dd/yy",provider), DateTo = DateTime.ParseExact("01/13/19","MM/dd/yy",provider) },
    new LogicalPeriod { DateFrom = DateTime.ParseExact("01/13/19","MM/dd/yy",provider), DateTo = DateTime.ParseExact("01/20/19","MM/dd/yy",provider) },
    new LogicalPeriod { DateFrom = DateTime.ParseExact("01/20/19","MM/dd/yy",provider), DateTo = DateTime.ParseExact("01/27/19","MM/dd/yy",provider) },
    new LogicalPeriod { DateFrom = DateTime.ParseExact("01/27/19","MM/dd/yy",provider), DateTo = DateTime.ParseExact("02/03/19","MM/dd/yy",provider) },
    new LogicalPeriod { DateFrom = DateTime.ParseExact("02/03/19","MM/dd/yy",provider), DateTo = DateTime.ParseExact("02/10/19","MM/dd/yy",provider) },
};


var result = logicalPeriods.GroupJoin(timeValues,
    p => p,
    t => logicalPeriods.FirstOrDefault(l => t.time > l.DateFrom && t.time <= l.DateTo),
    (p, times) => new {
        p.DateTo,
        value = times.Count() > 0 ? (double?)times.Sum(t => t.value) : null
    }
);

达到预期结果

DateTo   | value
12/23/18 | null
12/30/18 | 5.6
01/06/19 | 11.2
01/13/19 | 5.6
01/20/19 | 5.6
01/27/19 | null
02/03/19 | 5.6
02/10/19 | null

但是,正如@IvanStoev的评论中所述

  

事实(因此得到答案)是,对于这种类型的问题,没有好的标准LINQ解决方案(尽管有有效的非LINQ算法解决方案)。

使用该方法,我基本上可以达到相同的结果

var result = logicalPeriods.Select(p => new 
{ 
    p.DateTo, 
    value = timeValues.Where(t => t.time > p.DateFrom && t.time <= p.DateTo).Sum(t => t.value) 
}).ToList();

虽然产生的结果与原始解决方案相似,但在处理大型结果集时效率都不高。