User987
User987

Reputation: 3823

Grouping by list of dates and summing the sales

I have an situation where I have defined a class like following:

class Item
{
List<DateTime> _TransactonDatesPast30Days =  new List<DateTime>();
}

So the situation can be like following:

I have 25 transaction dates in the list... 

Next item:

40 transaction dates 

So now I have a List of items with all these parameters like following:

List<Item> _items = new List<Item>();

Now I need to group by the list within a list to get number of sales per day in final list...

For example:

12th of December occured 14 times in all List and now it's count is 14 , which will represent the number of sales made for that date...

In normal circumstances I would group them by like this:

ViewBag.LineGraph = lista
.GroupBy(l => l.TrasnactionDate)
.Select(cl => new Item
{
TrasnactionDate = cl.Key.TrasnactionDate.Date,
SaleNumber = cl.Count(c => cl.Key)
})
.OrderBy(x => x.TrasnactionDate)
.Where(x=>x.SaleNumber>0)
.ToList();

But it confuses me when I have list within a list that I need to group by, I have no ideas what to do now here when I have a list within a list that needs to grouped by the date...

Upvotes: 0

Views: 51

Answers (1)

FakeisMe
FakeisMe

Reputation: 464

// This wasn't grouping I guess

 var result = _items.Select(x => x._TransactonDatesPast30Days).GroupBy(y => y).Select(x => new Item {_TransactonDatesPast30Days =  x.Key ,Salesnumber = x.Count()});

Take this Solution: Update 2

   var resultt2 = _items.Select(x => x._TransactonDatesPast30Days).
                Select(r => r.GroupBy(x => x).Select(l => new Item { _TransactonDatesPast30Days = new List<DateTime>{l.Key}, Salesnumber = l.Count() }));

Upvotes: 1

Related Questions