void Main()
{
List<CustData> myList = GetCustData();
var query = myList
.GroupBy(c => c.CustId)
.Select(g => new
{
CustId = g.Key,
Jan = g.Where(c => c.OrderDate.Month == 1).Sum(c => c.Qty),
Feb = g.Where(c => c.OrderDate.Month == 2).Sum(c => c.Qty),
March = g.Where(c => c.OrderDate.Month == 3).Sum(c => c.Qty),
});
query.Dump();
}
public List<CustData> GetCustData()
{
List<CustData> custData = new List<CustData>
{
new CustData
{
CustId = 1,
OrderDate = new DateTime(2008, 1, 1),
Qty = 100
},
new CustData
{
CustId = 2,
OrderDate = new DateTime(2008, 1, 2),
Qty = 200
},
new CustData
{
CustId = 1,
OrderDate = new DateTime(2008, 2, 2),
Qty = 350
},
new CustData
{
CustId = 2,
OrderDate = new DateTime(2008, 2, 28),
Qty = 221
},
new CustData
{
CustId = 1,
OrderDate = new DateTime(2008, 3, 12),
Qty = 250
},
new CustData
{
CustId = 2,
OrderDate = new DateTime(2008, 3, 15),
Qty = 2150
},
};
return custData;
}
public class CustData
{
public int CustId;
public DateTime OrderDate;
public uint Qty;
}