I've tried using the the options from this SO question. I'm still receiving the following error though:
The specified type member 'DayOfWeek' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
Question now edited to contain full code of what I'm trying to achieve. It basically required to pull the average number of SMS's sent on each day of the week.
Here is my code:
var weekDays = rad.SMSSentItems
.Where(x => x.Status == "DELIVRD")
.GroupBy(x => x.StatusDate.Value.DayOfWeek)
.Select(g => new
{
DayOfWeek = g.Key,
Count = g.Count()
})
.ToList();
int avgSMSPerMonday = 0;
int avgSMSPerTuesday = 0;
int avgSMSPerWednesday = 0;
int avgSMSPerThursday = 0;
int avgSMSPerFriday = 0;
int avgSMSPerSaturday = 0;
int avgSMSPerSunday = 0;
int totalSMSPerDay = 0;
foreach (var day in weekDays)
{
totalSMSPerDay = rad.SMSSentItems.Where(x => (DbFunctions.TruncateTime(x.StatusDate).Value.DayOfWeek == day.DayOfWeek) && (x.Status == "DELIVRD")).ToList().Count;
if (day.DayOfWeek == DayOfWeek.Monday)
avgSMSPerMonday = totalSMSPerDay / day.Count;
else if (day.DayOfWeek == DayOfWeek.Tuesday)
avgSMSPerTuesday = totalSMSPerDay / day.Count;
else if (day.DayOfWeek == DayOfWeek.Wednesday)
avgSMSPerWednesday = totalSMSPerDay / day.Count;
else if (day.DayOfWeek == DayOfWeek.Thursday)
avgSMSPerThursday = totalSMSPerDay / day.Count;
else if (day.DayOfWeek == DayOfWeek.Friday)
avgSMSPerFriday = totalSMSPerDay / day.Count;
else if (day.DayOfWeek == DayOfWeek.Saturday)
avgSMSPerSaturday = totalSMSPerDay / day.Count;
else if (day.DayOfWeek == DayOfWeek.Sunday)
avgSMSPerSunday = totalSMSPerDay / day.Count;
}
Updated code using Camillo's code:
var weekDays = rad.SMSSentItems
.Where(x => x.Status == "DELIVRD")
.GroupBy(x => x.StatusDate.Value.DayOfWeek)
.Select(g => new
{
DayOfWeek = g.Key,
Count = g.Count()
})
.ToDictionary(x => x.DayOfWeek, x => x.Count);
int mondayCount = 0;
int tuesdayCount = 0;
int wednessdayCount = 0;
int thursdayCount = 0;
int fridayCount = 0;
int saturdayCount = 0;
int sundayCount = 0;
//determine average number of sms's sent per day of week
foreach (var day in weekDays)
{
int daysCount = rad.SMSSentItems
.Where(x => (x.StatusDate.Value.DayOfWeek == day.Key)
&& x.Status == "DELIVRD")
.Count();
if (day.Key == DayOfWeek.Monday)
mondayCount = daysCount / day.Value;
else if (day.Key == DayOfWeek.Tuesday)
tuesdayCount = daysCount / day.Value;
else if (day.Key == DayOfWeek.Wednesday)
wednessdayCount = daysCount / day.Value;
else if (day.Key == DayOfWeek.Thursday)
thursdayCount = daysCount / day.Value;
else if (day.Key == DayOfWeek.Friday)
fridayCount = daysCount / day.Value;
else if (day.Key == DayOfWeek.Saturday)
saturdayCount = daysCount / day.Value;
else if (day.Key == DayOfWeek.Sunday)
sundayCount = daysCount / day.Value;
}
I'm still left with the initial error I get from the 1st query which is as follows:
The specified type member 'DayOfWeek' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported