gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:22:16 25 4
gpt4 key购买 nike

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

最佳答案

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

关于c# - LINQ to Entities 无法使用查询语法识别 ToString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32977999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com