简体   繁体   English

类型为Dictionary的C#比较运算符不支持<string,string>

[英]C# Comparison operators not supported for type Dictionary<string,string>

I get a dictionary and i want to get the matching database entries for the key field in the dictionary. 我得到一本字典,我想获得字典中关键字段的匹配数据库条目。 So the code below gets all database fields that equals the data in the dictionary.keys. 因此,下面的代码获取的所有数据库字段都等于dictionary.keys中的数据。 So far so good. 到现在为止还挺好。 Now when i loop it and i try to get the field from the fields that matches the key x it fails with an exception: 现在,当我循环它并且我尝试从与键x匹配的字段中获取字段时,它失败并出现异常:

{"Comparison operators not supported for type 'System.Collections.Generic.Dictionary`2+KeyCollection[System.String,System.String]'"} {“类型'System.Collections.Generic.Dictionary`2 + KeyCollection [System.String,System.String]'}不支持比较运算符

var fields = _dc.fieldInfos.Where(x => x.name.Equals(param.MethodData.Keys));
foreach (var entry in param.MethodData)
{
    KeyValuePair<string, string> entry1 = entry;
    var field = fields.SingleOrDefault(x => x.name.Equals(entry1.Key));
    if (field == null)
    ....
}

The line that fails is this: 失败的是这样的:

var field = fields.SingleOrDefault(x => x.name.Equals(entry1.Key));

Any ideas? 有任何想法吗?

This is because the LINQ to SQL provider cannot translate the C# expression passed to the SingleOrDefault method into an SQL statement. 这是因为LINQ to SQL提供程序无法将传递给SingleOrDefault方法的C#表达式转换为SQL语句。

You can easily solve this problem by having LINQ to SQL execute the fields query before filtering it further with the SingleOrDefault method. 通过让LINQ to SQL执行fields查询, 然后使用SingleOrDefault方法进一步过滤它,可以轻松解决此问题。 This way the second operation will happen on the collection of objects in memory instead of the database: 这样,第二个操作将发生在内存中的对象集合而不是数据库上:

var field = fields.ToList().SingleOrDefault(x => x.name.Equals(entry1.Key));

Related resources: 相关资源:

Be mindful that the _dc.fieldInfos.Where(x => x.name.Equals(param.MethodData.Keys)) (line 1) doesn't actually get executed until you try to iterate over fields (line 5). 请注意,在尝试迭代fields (第5行)之前, _dc.fieldInfos.Where(x => x.name.Equals(param.MethodData.Keys)) (第1行) 实际上并未执行

At that point, x.name.Equals(param.MethodData.Keys) fails because a string (x.name) cannot be compared to KeyCollection<string> (param.MethodData.Keys). 此时, x.name.Equals(param.MethodData.Keys)失败,因为无法将string (x.name)与KeyCollection<string> (param.MethodData.Keys)进行比较。

Essentially, you are getting the exception on unexpected line because of the deferred execution. 基本上,由于延迟执行,您在意外行上获得异常。

Are you sure it's not this line that's failing: 你确定这条线不是失败的吗?

var fields = _dc.fieldInfos.Where(x => x.name.Equals(param.MethodData.Keys)); 

as name isn't going to match a collection of strings. 因为name不会匹配字符串集合。

which if so, I'd rewrite as: 如果是这样,我会改写为:

var fields = _dc.fieldInfos.Where(x => param.MethodData.Keys.Contains(x.name));

entry1.key is a string, so I can't see why the line that you highlighted would fail with that message. entry1.key是一个字符串,所以我看不出为什么你突出显示的行会因该消息而失败。

var fields = _dc.fieldInfos.Where(x => x.name.Equals(param.MethodData.Keys));

This retrieves (actually filters with deferred execution) fieldInfos by matching their name to a collection of keys. 这通过将其名称与键集合进行匹配来检索(实际上是使用延迟执行的过滤器) fieldInfos I don't know what fieldInfo.Name and param.MethodData.Keys mean, but i assume it's not what you want. 我不知道fieldInfo.Nameparam.MethodData.Keys是什么意思,但我认为它不是你想要的。

I'd suggest something along the lines of: 我建议采取以下措施:

var pairs = param.MethodData.Join(_dc.fieldInfos, 
    kvp => kvp.Key, 
    fi => fi.name, 
    (kvp, fi) => new { Key = kvp.Key, FieldInfo = fi });

foreach (var pair in pairs)
    param.MethodData[pair.Key] = pair.FieldInfo;

Im not exactly sure it's gonna work in Linq to SQL though. 我不确定它会在Linq to SQL中工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM