在实体框架中检索数据时,类型为“System.NotSupportedException”的例外

时间:2015-04-20 23:55:53

标签: linq entity-framework

我有一个名为ServiceDayWithEmployee的类,其中包含其他类DaysWithSubCount的列表 这里两个班级的样子......

public class ServiceDayWithEmployee
        {
            public int shiftID { get; set; }
            public string shiftName { get; set; }
            public int serviceDaysCount { get; set; }
            public List<DaysWithSubCount> days{ get; set; }

        }

        public class DaysWithSubCount
        {
              public int count { get; set; }
              public DateTime date { get; set; }

        }

我想要运行一个查询来填充数据库中的ServiceDayWithEmployee,包括当天的列表。

我在foreach循环中这样做,如下所示......

foreach(shift sh in shifts)
            {
                ServiceDayWithEmployee SDE= new ServiceDayWithEmployee();
                SDE.shiftID=sh.id;
                SDE.shiftName=sh.shift_name;
                SDE.days = fleet.subscribtion_dates.Where(x => x.shift.Equals(sh.id)).GroupBy(x => x.date).Select(grp => new DaysWithSubCount { date = grp.Key.Value, count = grp.Count() }).ToList();
                SDE.serviceDaysCount=SDE.days.Count;
                SDEs.Add(SDE);
            }

这行代码给了我一个例外,如果我使用var查询而不是SDE.days并删除.ToList()它不会崩溃,但我不知道如何填写日期列表然后

 SDE.days = fleet.subscribtion_dates.Where(x => x.shift.Equals(sh.id)).GroupBy(x => x.date).Select(grp => new DaysWithSubCount { date = grp.Key.Value, count = grp.Count() }).ToList();

以下是例外......

An exception of type 'System.NotSupportedException' occurred in System.Data.Entity.dll but was not handled in user code

Additional information: Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context. 

1 个答案:

答案 0 :(得分:2)

错误听起来像表达式无法转换为SQL。

我的猜测是问题在于x.shift.Equals(sh.id)如果EF试图引入x.shiftx,如果直接比较ID,它就会起作用。

相关问题