LINQ to Entities 无法使用查询语法识别 ToString

LINQ to Entities does not recognize ToString using query syntax

我在 LINQ 查询(使用查询语法)中收到此错误。过去,我在 LINQ 查询中使用点语法时遇到过此错误,所以我所要做的就是调用 ToList(),然后使用匿名 class 调用 select。但是在我现在使用查询语法的情况下,我该如何做同样的事情呢?是的,我正在使用 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();

使用 string.Format

这样的东西可能更有意义
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),

您可以只 select "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),
                   ...
               });

仅供参考。这是您需要做的通用表格:

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;