LINQ到实体不能使用查询语法识别ToString

本文关键字:查询 语法 识别 ToString 实体 不能 LINQ | 更新日期: 2023-09-27 18:16:23

我在LINQ查询(使用的查询语法)中得到此错误。在过去,当我在LINQ查询中使用点语法时,我得到了这个错误,所以我所要做的就是调用ToList(),然后使用匿名类进行选择。但在我现在使用查询语法的情况下,我如何做同样的事情?是的,我正在使用EF。

代码如下:

            var dataList = from h in context.Horaires
            join e in context.Employes on h.iIdEmploye equals e.iIdEmploye
            join p in context.Postes on h.iIdPoste equals p.iIdPoste
            join g in context.GroupesPostes on p.iIdGroupePoste equals g.iIdGroupePoste
            join en in context.EnsemblesPostes on g.iIdEnsemblePoste equals en.iIdEnsemblePoste
            join d in context.Departements on e.iIdDepartement equals d.iIdDepartement
            where p.bitActif == true && h.dteDate == p.dteDate
            orderby e.sIdEmployeClient
            select new ScenarioScheduleItemModel
            {
                H = "D",
                EmployeeSchedule = "EmployeeSchedule",
                EmployeeSchedule2 = "EmployeeSchedule",
                EmployeeXrefCode = e.sIdEmployeClient,
                // ToString used here for StartTime and EndTime
                StartTime = h.dteDate.ToString(CultureInfo.InvariantCulture).Substring(0, 10) + "T" + p.dteHeureDebut.ToString(CultureInfo.InvariantCulture).Substring(11, 8),
                EndTime = h.dteDate.ToString(CultureInfo.InvariantCulture).Substring(0, 10) + "T" + p.dteHeureFin.ToString(CultureInfo.InvariantCulture).Substring(11, 8),
                DeptXrefCode = d.sNom,
                JobXrefCode = p.sNom,
                OrgUnit = string.Empty,
                XrefCode = string.Empty,
                OrgLocationTypeXrefCode = string.Empty,
                PayAdjCodeXrefCode = string.Empty
            };
        var result = dataList.Distinct().ToList();

LINQ到实体不能使用查询语法识别ToString

您可以选择"raw"值,然后使用AsEnumerableSelect来获得所需的值

var dataList = (from h in context.Horaires
               ...
               select new { e, h, p, d }).AsEnumerable()
               .Select(anon => new ScenarioScheduleItemModel
               {
                   ...
                   StartTime = anon.h.dteDate.ToString(CultureInfo.InvariantCulture)
                                   .Substring(0, 10) 
                        + "T" + anon.p.dteHeureDebut.ToString(CultureInfo.InvariantCulture)
                                    .Substring(11, 8),
                   EndTime = anon.h.dteDate.ToString(CultureInfo.InvariantCulture)
                                 .Substring(0, 10) 
                        + "T" + anon.p.dteHeureFin.ToString(CultureInfo.InvariantCulture)
                                    .Substring(11, 8),
                   ...
               });

使用string之类的东西可能更有意义。格式的

StartTime = string.Format("{0:MM/dd/yyyy}T{1:hh:mm:ss}", h.dteDate, p.dteHeureDebut),
EndTime = string.Format("{0:MM/dd/yyyy}T{1:hh:mm:ss}", h.dteDate, p.dteHeureFin),

仅供参考。下面是你需要做的一般形式:

var dbQuery = from x in db.Table
              // do stuff with x what will be translated to SQL
              select x;
var memoryQuery = from z in dbQuery.AsEnumerable() // <-- !
                  // do stuff with z in memory
                  select z;