简体   繁体   English

LINQ to Entities 无法识别方法“System.Collections.Generic.Dictionary”2[System.Int32,System.String] ToDictionary

[英]LINQ to Entities does not recognize the method 'System.Collections.Generic.Dictionary`2[System.Int32,System.String] ToDictionary

I am trying to retrieve list of EmployeeDTO from DB which are stored in Employee table.我试图检索列表EmployeeDTO从数据库中存储的Employee表。 Every employee can have one or more specialty.每个员工都可以有一个或多个专长。 Specialty are stored in OrganizationSpecialtyType .专业存储在OrganizationSpecialtyType Employee and OrganizationSpecialtyType are related with "many-to-many" via EmployeeSpecialty table. EmployeeOrganizationSpecialtyType通过EmployeeSpecialty表与“多对多”相关联。

I'm using the following query for that, and got an exception like in title:我为此使用了以下查询,并得到了标题中的异常:

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)
                                    }
                        );

In the EmployeeDTO class the property Specialty is type public Dictionary<int, string> .EmployeeDTO类中,属性Specialty的类型为public Dictionary<int, string>

If I execute this query, everything works normally:如果我执行这个查询,一切正常:

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

How I can solve my first query to obtain EmployeeDTO with specialties?我如何解决我的第一个查询以获取具有专业知识的EmployeeDTO

Thanks.谢谢。

You can select to anonymous type first, then set the dictionary later.您可以先选择匿名类型,然后再设置字典。

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;
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LINQ to Entities无法识别方法&#39;System.Collections.Generic.Dictionary` - LINQ to Entities does not recognize the method 'System.Collections.Generic.Dictionary` LINQ to Entities无法识别方法'Int32 IndexOf(System.String,System.StringComparison)'方法 - LINQ to Entities does not recognize the method 'Int32 IndexOf(System.String, System.StringComparison)' method LINQ to Entities无法识别方法&#39;Int32 ToInt32(System.String)&#39;方法 - LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method LINQ to Entities无法识别方法&#39;System.String GetMonthName(Int32)&#39;方法 - LINQ to Entities does not recognize the method 'System.String GetMonthName(Int32)' method LINQ to Entities无法识别方法&#39;Int32 CompareTo(System.String)&#39;方法 - LINQ to Entities does not recognize the method 'Int32 CompareTo(System.String)' method LINQ to Entities 无法识别“System.String get_Item(Int32)”方法 - LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method LINQ to Entities无法识别方法&#39;System.String ToString(Int32)&#39;方法 - LINQ to Entities does not recognize the method 'System.String ToString(Int32)' method LINQ to Entities无法识别方法&#39;Int32 Parse(System.String)&#39;方法, - LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, LINQ 到 Entities 无法识别方法 'System.String SetAssetStatus(Int32)' 方法 - LINQ to Entities does not recognize the method 'System.String SetAssetStatus(Int32)' method LINQ to Entities无法识别方法&#39;Int32 Parse(System.String)&#39;为什么? - LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM