I have written a following code which basically groups my sales by days and then I show them to users on graph like following:
var DailyGraph = userStores.ToList().GroupBy(x => x.TransactionDate.Value.DayOfWeek).Select(pr => new { Day = pr.Key.ToString(), Sales = pr.Sum(x => x.QuantitySoldTransaction) });
var dayIndex = new List<string> { "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY" };
ViewBag.DailyGraph = DailyGraph.OrderBy(e => dayIndex.IndexOf(e.Day.ToUpper())).ToList();
Okay so what happens here, in first line I group all of the available sales by day of the week parameter which is Enumeration... Then I create a list of strings with exact day names like the Enumeration , I compare them so that I can order them in exact line as days go (from Monday to Sunday)...
My problem here is now that if user didn't had sales in specific days... Let's say Monday and Thursday he didn't have sales... How could I now add these missing days to my list here ? I'm a little bit confused on how do to that since i'm working with enumeration...
So the current output would be:
Tuesday 5 sales
Wednesday 9 sales
Friday 4 sales
Saturday 13 sales
Sunday 5 sales
And the desired output is:
Monday 0 sales // Add missing day enumeration and assign 0 value
Tuesday 5 sales
Wednesday 9 sales
Thursday 0 sales // Add missing day enumeration and assign 0 value
Friday 4 sales
Saturday 13 sales
Sunday 5 sales
Can someone help me out?
Dan , something like this?
for (int i = 0; i < DailyGraph.Count(); i++)
{
if (!dayIndex.Contains(DailyGraph[i].Day.ToUpper()))
{
DailyGraph.Add(new HourlyGraph { Day = dayIndex.ElementAt(i), Sales = 0 });
}
}