简体   繁体   English

从var转换为字典<String,int>

[英]converting from var to dictionary<String,int>

I have a dictionary defined as Dictionary<string, int> one 我有一个字典定义为Dictionary<string, int> one

it has some data and I do the following LINQ on it 它有一些数据,我在上面做LINQ

var temp = one.OrderBy(r => r.Value).Select(r => r.Key).Take(5);

But now I want to convert it back into a `Dictionary 但现在我想将它转换回`Dictionary

I tried using temp.ToDictionary(r => r.Key, r => r.Value); 我尝试使用temp.ToDictionary(r => r.Key, r => r.Value);

But it tells me: Cannot convert lambda expression to type 'System.Collections.Generic.IEqualityComparer' because it is not a delegate type 但它告诉我:无法将lambda表达式转换为'System.Collections.Generic.IEqualityComparer'类型,因为它不是委托类型

How do I do this conversion? 我该如何进行转换?

It's because you throw away the Value with the call to Select(r => r.Key) . 这是因为你通过调用Select(r => r.Key)扔掉了Value You'll need to keep the KeyValuePair together if you want to convert it back to a dictionary. 如果要将KeyValuePair转换回字典,则需要将KeyValuePair保持在一起。

var temp = one.OrderBy(r => r.Value).Take(5);

var backToDictionary = temp.ToDictionary(r => r.Key, r => r.Value);

If you still want to have an IEnumerable<string> of the keys as in your question, you can use this separately: 如果您仍然希望在问题中拥有密钥的IEnumerable<string> ,则可以单独使用:

var tempKeys = temp.Select(r => r.Key); 

The reason you're receiving a seemingly unrelated error message referring to an IEqualityComparer is because the compiler is attempting to make a best guess as to which overload you are attempting to call. 您收到引用IEqualityComparer的看似无关的错误消息的原因是因为编译器试图最好地猜测您尝试调用哪个重载。 Its best guess in this case thinks you were trying to call this overload . 在这种情况下它最好的猜测认为你试图调用这个过载

Consider the code you had and the type it produced: 考虑一下您拥有的代码及其生成的类型:

IEnumerable<string> temp = one.OrderBy(r => r.Value).Select(r => r.Key).Take(5);

This would produce an object implementing IEnumerable<string> . 这将产生一个实现IEnumerable<string>的对象。 Then with your call of: 然后你的电话:

temp.ToDictionary(r => r.Key, r => r.Value);

r in this case is string . 在这种情况下, r string The compiler at this point is freaking out because there's no such thing as r.Key nor r.Value . 此时的编译器吓坏了,因为没有r.Keyr.Value这样的东西。 It recognizes that there are 2 parameters being used, and thus has two possible overloads to pick from for ToDictionary ( this method , and this one ). 它识别出有两个参数被使用,因此有两个可能的重载来从ToDictionary这个方法这个 )。 At this point, I'm not sure what the rules are for the compiler to choose one over the other (especially since it cannot infer the types of r.Key or r.Value ), but it chose one of them. 此时,我不确定编译器选择其中一个的规则是什么(特别是因为它无法推断r.Keyr.Value的类型),但它选择了其中一个。 (perhaps it's simply the "first" one declared/found? Perhaps it favours direct object inputs over lambda expressions?) At any rate, it chooses the overload requiring an IEqualityComparer instead of a Func<TSource, TElement> and tells you (naturally) that a lambda expression is not convertable to IEqualityComprarer . (也许它只是声明/找到的“第一个”?也许它有利于直接对象输入而不是lambda表达式?)无论如何,它选择需要IEqualityComparer而不是Func<TSource, TElement>的重载并告诉你(自然) lambda表达式不能转换为IEqualityComprarer

In my experience, once you feed the compiler garbage ( r.Key and r.Value in this case), overload resolution kinda goes out the window. 根据我的经验,一旦你输入编译器垃圾(在这种情况下为r.Keyr.Value ),重载r.Value有点会消失。 Sometimes it works out, usually when there is only one overload that matches the number parameters, or at least no ambiguity. 有时它会运行,通常只有一个重载与数字参数匹配,或者至少没有歧义。 But other times you just gotta see past the compiler error and fix the root problem. 但有时你只需要看看编译器错误并解决根问题。

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

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