【问题标题】:Converting string to nullable DayOfWeek field using Linq cause an error使用 Linq 将字符串转换为可为空的 DayOfWeek 字段会导致错误
【发布时间】:2018-03-20 23:24:46
【问题描述】:

在我的 sql 数据库中,WorkDay 字段为字符串格式,在模型中为nullable DayOfWeek,即public DayOfWeek? WorkDay { get; set; }。将数据库WorkDay 字段转换为模型WorkDay 字段时,会生成如下错误:

无法将表达式“Table(StaffSchedule)”转换为 SQL 和 无法将其视为本地表达式。

我还尝试创建三个不同的 linq 语句,如下所示。

1) 从 StaffSchedule 表中检索数据。
2) 对其应用选择操作。
3) 对选定的数据应用 AddRange 操作。

results.AddRange(dataContext.StaffSchedules
                            .Where(x => !x.Excluded)
                            .Where(x => x.DistrictID == districtId && x.District.Active && (x.Position == positionTeacher || x.Position == positionDirector || x.Position == positionAssistant || x.Position == positionAssistantDirector))
                           .Select(x => new Core.StaffSchedule()
                            {
                                ID = x.ID,
                                Staff = x.Staff.SelectSummary(),
                                Position = (StaffPosition)Enum.Parse(typeof(StaffPosition), x.Position, true),
                                Class = refs.Class,
                                District = x.District.SelectSummary(),
                                Time = null,
                                Reoccurring = false,
                                Inherited = true,
                                ReoccourringStart = x.ReoccourringStart,
                                ReoccourringEnd = x.ReoccourringEnd,
                                WorkDay = x.WorkDay == null ? (DayOfWeek?)null : (DayOfWeek)Enum.Parse(typeof(DayOfWeek), x.WorkDay, true)
                            }));

这是stringnullable DayOfWeek 字段的转换代码。这在我的情况下会导致错误。

WorkDay = x.WorkDay == null ? (DayOfWeek?)null : (DayOfWeek)Enum.Parse(typeof(DayOfWeek), x.WorkDay, true) 

我已经浏览了下面的链接。

【问题讨论】:

  • 为什么在异常消息中显示“Table(StaffSchedule)”? dataContext.StaffSchedules确切类型是什么?

标签: c# entity-framework linq


【解决方案1】:

尝试通过调用ToList()dataContext.StaffSchedules 转换为IEnumerable 像这样进行查询之前的方法

results.AddRange(dataContext.StaffSchedules.ToList()
                        .Where(x => !x.Excluded)....the rest of you query 

搜索IEnumerableIQueryable的区别,更详细的解释

【讨论】:

    【解决方案2】:

    您无法将任何 C# 代码转换为 SQL,因此 x.WorkDay == null ? (DayOfWeek?)null : (DayOfWeek)Enum.Parse(typeof(DayOfWeek), x.WorkDay, true) 无法在 Linq to Entities 中工作。

    通过在Select 之前写入AsEnumerable(),尝试在查询执行后选择您的数据。不要在查询开始时这样做,因为您将从 db 表中获取所有数据。

    results.AddRange(dataContext.StaffSchedules
        //everything (well almost) from this point is going to be translated into SQL
        .Where(x => !x.Excluded)
        .AsEnumerable() //everything from here is going to be executed after the query ends so it can be any C# code
        .Select(x => new Core.StaffSchedule()
        {
            //now this should work
            WorkDay = x.WorkDay == null ? (DayOfWeek?)null : (DayOfWeek)Enum.Parse(typeof(DayOfWeek), x.WorkDay, true)
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-22
      • 1970-01-01
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-23
      相关资源
      最近更新 更多