连续日期组合成范围 [英] Combining consecutive dates into ranges

查看:111
本文介绍了连续日期组合成范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对象的List

 公共类样品
{
公共DateTime的日期;
公共字符串的内容;
}



我希望能够创建新的对象列表



 公共类sampleWithIntervals 
{
公众的DateTime的startDate;
公共DateTime的结束日期;
公共字符串的内容;
}



样品物体应被分组为基于所述内容的时间间隔。该时间间隔可以只包括包含在原始样本名单的日期。
我不知道如何在Linq的做到这一点。



示例数据:

  {2013年10月1日,×} 
{2013年10月2日,×}
{2013年10月2日, y的}
{2013年10月3日,×}
{2013年10月3日,y的}
{2013年10月10日,的x}
{2013年10月11日,×}
{2013年10月15日,y的}
{2013年10月16日 ,Y}
{二零一三年十月二十日,Y}

本应该给我
{2013年10月1日,10 /二千〇一十三分之三,×}
{2013年10月2日,2013年10月3日,y的}
{2013年10月10日,10 /二千零一十三分之十一,×}
{2013年10月15日,2013年10月16日,y的}
{二零一三年十月二十○日,10 /二千零一十三分之二十〇,Y}


解决方案

下面是一个非LINQ的方式来做到这一点:

 列表< sampleWithIntervals>组=新的List< sampleWithIntervals>(); 
sampleWithIntervals curGroup = NULL;

的foreach(样本S在samples.OrderBy(SA => sa.content).ThenBy(SA => sa.Date))
{
如果(curGroup = = NULL || //第一组
s.Date!= curGroup.endDate.AddDays(1)||
s.content!= curGroup.content //新集团

{
curGroup =新sampleWithIntervals(){=的startDate s.Date,结束日期= s.Date,内容= s.content};
groups.Add(curGroup);
}
,否则
{
//添加到当前组
curGroup.endDate = s.Date;
}
}

您可以使用一个技巧,团体使用LINQ做到这一点项目的日期减去指数组连续的项目:

  samples.OrderBy(S = GT; s.content)
.ThenBy(S = GT; s.Date)
//选择与它的索引
。选择每个项目((S,I)=>新建{样品= S,指数= I })
按日期miuns指数组连续的项目
.GroupBy //组(SI =>新建{日期= si.sample.Date.AddDays(-si.index),内容= SI。 sample.content})
//获取最小值,最大值,每组
。选择的含量(g =>新建sampleWithIntervals(){
的startDate = g.Min(S = > s.sample.Date)
结束日期=栽培大豆(S =方式> s.sample.Date)
含量= g.First()sample.content
} )


I have a List of objects

public class sample
{
 public DateTime Date;
 public string content;
}

I want to be able to create a list of new objects

public class sampleWithIntervals
{
 public DateTime startDate;
 public DateTime endDate;
 public string content;
}

The sample objects should be grouped into intervals based on the content. The intervals can include only those dates that are included in the original sample list. I dont know how to do this in Linq.

Sample data:

{"10/1/2013", "x"}
{"10/2/2013", "x"}
{"10/2/2013", "y"}
{"10/3/2013", "x"}
{"10/3/2013", "y"}
{"10/10/2013", "x"}
{"10/11/2013", "x"}
{"10/15/2013", "y"}
{"10/16/2013", "y"}
{"10/20/2013", "y"}

This should give me 
{"10/1/2013","10/3/2013", "x"}
{"10/2/2013","10/3/2013", "y"}
{"10/10/2013","10/11/2013", "x"}
{"10/15/2013","10/16/2013", "y"}
{"10/20/2013","10/20/2013", "y"}

解决方案

Here's a non-Linq way to do it:

List<sampleWithIntervals> groups = new List<sampleWithIntervals>();  
sampleWithIntervals curGroup = null;

foreach(sample s in samples.OrderBy(sa => sa.content).ThenBy(sa => sa.Date))
{
    if(curGroup == null || // first group
        s.Date != curGroup.endDate.AddDays(1) ||
        s.content != curGroup.content   // new group
      ) 
    {
        curGroup = new sampleWithIntervals() {startDate = s.Date, endDate = s.Date, content = s.content};
        groups.Add(curGroup);
    }
    else
    {
        // add to current group
        curGroup.endDate = s.Date;
    }
}

You can do this with Linq using a trick that groups the items by the date minus the index to group consecutive items:

samples.OrderBy(s => s.content)   
       .ThenBy(s => s.Date)
       // select each item with its index
       .Select ((s, i) => new {sample = s, index = i})  
       // group by date miuns index to group consecutive items
       .GroupBy(si => new {date = si.sample.Date.AddDays(-si.index), content = si.sample.content})  
       // get the min, max, and content of each group
       .Select(g => new sampleWithIntervals() {
                        startDate = g.Min(s => s.sample.Date), 
                        endDate = g.Max(s => s.sample.Date), 
                        content = g.First().sample.content
                        })

这篇关于连续日期组合成范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆