1

我有一个场景,我需要List<my_custom_object>通过基于公共列提取 anIQueryable<T>和 a之间的匹配属性来生成一个新的Dictionary<String, String>

这是我的字典:

DirectoryProfilePropertiesCollection = (Dictionary<String, String>)ApiMethods.GetProfileProperties(OrganizationIdentifier, DirectoryIdentifier, DirectoryUserName, DirectoryUserPassword);

这将返回以下信息:

Key: samAccountName, Value: hgulzar 
Key: sn,             Value: Hassan
Key: lastname,       Value: Gulzar

这里的键称为属性名称。

我也有一个IQueryable

var DirectoryMappingsCollection = repository.GetQuery<OrganizationDirectoryMapping>(where => where.OrganizationDirectory.Organization.OrganizationUnit.UnitIdentifier == OrganizationIdentifier && where.OrganizationDirectory.DirectoryIdentifier == DirectoryIdentifier);

OrganizationDirectoryMapping实体有一个名为DirectoryAttribute

当我要匹配它们时,这两个集合都有数据。的DirectoryProfilePropertiesCollection对有AttributeNameAttributeValue。的DirectoryMappingsCollection实体, OrganizationDirectoryMapping, 具有AttributeNameDetailName

我需要获得一个自定义类对象,它代表:DetailName、、DirectoryAttributeAttributeValue

一直在尝试一些查询,但是当我遇到异常时卡住了:

Unable to create a constant value of type 'System.Collections.Generic.KeyValuePair`2'. Only primitive types or enumeration types are supported in this context.

LinQ 嵌套集合查询的问题对异常有点意义,但我仍然卡住了。

问候。

到目前为止我已经尝试过:

combinedCol = (from mapCol in DirectoryMappingsCollection
                                                join propCol in DirectoryProfilePropertiesCollection
                                                on mapCol.DirectoryAttribute equals propCol.Key
                                                select new CombinedMappingDataCollection
                                                {
                                                    DetailName = mapCol.DetailRequirement.DetailName,
                                                    AttributeName = propCol.Key,
                                                    AttributeValue = propCol.Value
                                                }).ToList();
4

1 回答 1

2

假设 DirectoryMappingsCollection 来自实体框架,您不能在查询中包含字典。实体框架无法将 KeyValuePair 转换为实际的 SQL 代码。

您可以将 .ToList 添加到 DirectoryMappingsCollection 以强制它获取查询中的整个表,它应该可以工作。如果您正在处理大型数据集,建议添加 .Where() 来限制数据。

combinedCol = (from mapCol in DirectoryMappingsCollection.ToList()
               join propCol in DirectoryProfilePropertiesCollection
               on mapCol.DirectoryAttribute equals propCol.Key
               select new CombinedMappingDataCollection
               {
                   DetailName = mapCol.DetailRequirement.DetailName,
                   AttributeName = propCol.Key,
                   AttributeValue = propCol.Value
               }).ToList();

请参阅linq to entity framework: use dictionary in query for a simular question。

于 2013-06-11T13:35:21.097 回答