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

时间:2015-10-06 19:21:45

标签: c# entity-framework linq linq-to-entities

我在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();

3 个答案:

答案 0 :(得分:2)

您可以选择“原始”值,然后使用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),
                   ...
               });

答案 1 :(得分:0)

使用像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),

答案 2 :(得分:0)

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

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;