gpt4 book ai didi

c# - LINQ to Entities 无法识别方法 'System.Collections.Generic.Dictionary` 2[System.Int32,System.String] ToDictionary

转载 作者:太空狗 更新时间:2023-10-29 22:37:07 25 4
gpt4 key购买 nike

我正在尝试检索 EmployeeDTO 的列表来自存储在 Employee 表中的数据库。每个员工都可以拥有一个或多个专长。专业存储在OrganizationSpecialtyType . EmployeeOrganizationSpecialtyType通过 EmployeeSpecialty 与“多对多”相关表。

我为此使用了以下查询,但出现了标题中的异常:

var q = _context.Employee.Where(p => employeeEMIIDs.Contains(p.EmployeeID))
.Select(p => new EmployeeDTO
{
EmployeeID = p.EmployeeID,
GenderTypeID = p.GenderTypeID,
FirstName = p.FirstName,
LastName = p.LastName,
Name = p.Name,
MiddleName = p.MiddleName,
DOB = p.DOB,
Suffix = p.Suffix,
Title = p.Title,
Specialty = p.EmployeeSpecialty
.ToDictionary(d => d.OrganizationSpecialtyType.SpecialtyTypeID, d => d.OrganizationSpecialtyType.Name)
}
);

EmployeeDTO对属性进行分类 Specialty类型是 public Dictionary<int, string> .

如果我执行这个查询,一切正常:

var spec = _context.Employee.Where(p => p.EmployeeID == -9070).FirstOrDefault()
.EmployeeSpecialty.ToDictionary(d =>
d.OrganizationSpecialtyType.SpecialtyTypeID,
d => d.OrganizationSpecialtyType.Name);

如何解决我的第一个查询以获得 EmployeeDTO有专长吗?

谢谢。

最佳答案

可以先选择匿名类型,再设置字典。

var q = _context.Employee.Where(p => employeeEMIIDs.Contains(p.EmployeeID))
.Select(p => new
{
Employee = new EmployeeDTO
{
EmployeeID = p.EmployeeID,
GenderTypeID = p.GenderTypeID,
FirstName = p.FirstName,
LastName = p.LastName,
Name = p.Name,
MiddleName = p.MiddleName,
DOB = p.DOB,
Suffix = p.Suffix,
Title = p.Title
},
Specialty = p.EmployeeSpecialty
.Select(d => new
{
d.OrganizationSpecialtyType.SpecialtyTypeID,
d.OrganizationSpecialtyType.Name
})
})
.AsEnumerable()
.Select(a =>
{
a.Employee.Specialty = a.Specialty
.ToDictionary(d => d.SpecialtyTypeID, d => d.Name);
return a.Employee;
});

关于c# - LINQ to Entities 无法识别方法 'System.Collections.Generic.Dictionary` 2[System.Int32,System.String] ToDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25923721/

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