This Code Help Me.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Aggregate
{
class MyData
{
public DateTime StartDate;
public DateTime EndDate;
public string type;
}
class Program
{
static void Main(string[] args)
{
List<mydata> myDataList = new List<mydata>();
myDataList.Add(new MyData() { StartDate = new DateTime(2008, 06, 19), EndDate = new DateTime(2008, 06, 25), type = "1" });
myDataList.Add(new MyData() { StartDate = new DateTime(2008, 06, 20), EndDate = new DateTime(2008, 06, 25), type = "1" });
myDataList.Add(new MyData() { StartDate = new DateTime(2008, 06, 20), EndDate = new DateTime(2008, 06, 28), type = "1" });
myDataList.Add(new MyData() { StartDate = new DateTime(2008, 06, 22), EndDate = new DateTime(2008, 06, 25), type = "2" });
myDataList.Add(new MyData() { StartDate = new DateTime(2008, 06, 22), EndDate = new DateTime(2008, 06, 26), type = "2" });
myDataList.Add(new MyData() { StartDate = new DateTime(2008, 06, 23), EndDate = new DateTime(2008, 06, 24), type = "3" });
myDataList.Add(new MyData() { StartDate = new DateTime(2008, 06, 19), EndDate = new DateTime(2008, 06, 27), type = "4" });
var result =
from recod in myDataList
group recod by recod.type into g
select new {type = g.Key, StartDate = g.Min(record => record.StartDate), EndDate = g.Max(record => record.EndDate) };
foreach (var row in result)
{
Console.WriteLine("{0}\t{1}\t{2}",
row.type,
row.StartDate.ToString(),
row.EndDate.ToString());
}
Console.ReadKey();
}
}
}