指定的类型成员' DayOfWeek' LINQ不支持

时间:2017-12-23 20:36:53

标签: c# entity-framework linq

我已尝试使用this SO question中的选项。我仍然收到以下错误:

The specified type member 'DayOfWeek' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

现在编辑的问题包含我尝试实现的完整代码。它基本上需要提取一周中每天发送的平均短信数量。

这是我的代码:

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;
}

使用Camillo的代码更新代码:

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;
                }

我还是留下了我从第一个查询得到的初始错误,如下所示:

The specified type member 'DayOfWeek' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported

1 个答案:

答案 0 :(得分:2)

您不需要进行所有检查,您可以使用Dictionary<DayOfWeek, int>代替:

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);

int mondayCount = weekDays[DayOfWeek.Monday];
int tuesdayCount = weekDays[DayOfWeek.Tuesday];
//...

如果您不确定某一天是否为某个值,请改用TryGetValue

weekDays.TryGetValue(DayOfWeek.Monday, out mondayCount);

如果你想要一周中某一天的平均值,你可以使用这样的东西:

foreach (var day in weekDays)
{
    int daysCount = rad.SMSSentItems
        .Where(x => x.StatusDate.Value.DayOfWeek == day.DayOfWeek
                 && x.Status == "DELIVRD")
        .Count();

    int dayAverage = day.Count / daysCount;
}