我的问题是:如何将lambda表达式的“var result”转换为可用的对象,然后我可以打印结果?
我还要感谢我在声明外部=>时对正在发生的事情的深入解释. outer.Value.Frequency
(我已经阅读了很多关于lambda表达式的解释,但是额外的澄清会有所帮助)
C# //Input : {5,13,6,5,7,8,5} //Output : {5,8} //The question is to arrange the numbers in the array in decreasing order of their frequency,preserving the order of their occurrence. //If there is a tie,like in this example between 13 and 6,then the number occurring first in the input array would come first in the output array. List<int> input = new List<int>(); input.Add(5); input.Add(13); input.Add(6); input.Add(5); input.Add(13); input.Add(7); input.Add(8); input.Add(6); input.Add(5); Dictionary<int,FrequencyAndValue> dictionary = new Dictionary<int,FrequencyAndValue>(); foreach (int number in input) { if (!dictionary.ContainsKey(number)) { dictionary.Add(number,new FrequencyAndValue(1,number) ); } else { dictionary[number].Frequency++; } } var result = dictionary.OrderByDescending(outer => outer.Value.Frequency); // How to translate the result into something I can print??
有关打印命令的答案,请参阅my answer here.
解决方法
how do I translate the “var result” of the lambda expression into a usable object that I can then print the result?
首先,“lambda表达式”仅是表达式的一部分,其形式为a => b.查询的其余部分只是一个以lambda作为参数的方法调用.
无论如何,如果我可以教人们关于LINQ的一件事,那就是:“结果”不是查询的结果,而是查询本身.
如果要查看结果,请询问每个结果的查询:
foreach(var item in result) Console.WriteLine(item.ToString());
I would also appreciate an in-depth explanation of what is happening when I declare the outer => outer.Value.Frequency
当然.我们首先计算所涉及的所有类型.我们看到lambda是一个函数,它接受keyvaluePair并返回一个int,因此我们生成一个方法
static private int MyLambda(keyvaluePair<int,FrequencyAndValue> outer) { return outer.Value.Frequency; }
接下来,我们采用该方法并从中创建委托:
var result = dictionary.OrderByDescending( new Func<keyvaluePair<int,FrequencyAndValue>,int>(MyLambda));
var result = Enumerable.OrderByDescending<keyvaluePair<int,int>( dictionary,new Func<keyvaluePair<int,int>(MyLambda));
并重写var:
IOrderedEnumerable<keyvaluePair<int,FrequencyAndValue>> result = Enumerable.OrderByDescending<keyvaluePair<int,int>(MyLambda));
我希望你同意你输入的代码比这个混乱更具可读性.类型推断岩石.
结果是一个对象,它表示通过给定键对此字典进行排序的能力.仔细阅读:它代表了通过该键对字典进行排序的能力.在你要求结果之前,它实际上并没有这样做;到目前为止,它只是一个对象,说“当被要求获得结果时,按此键对字典进行排序”.
假设你要求一个结果.它如何计算排序列表?它询问每个元素的字典.然后它在每个元素上调用MyLambda,它返回一个整数,所以我们现在有一对字典键值对和整数.然后它构建一个按该整数排序的对列表.然后,当你要求时,它会一次分发一个列表中的元素.
We see that the lambda is a function which takes a keyvaluePair and returns an int” – How did you determine that? I don’t see it from the method return value,nor documented in the OrderByDescending().
啊,我看到了混乱;出于教学原因,我对语义分析的确切顺序略微提出了一点.
我们如何进行这种类型的推理是C#中更微妙和有趣的部分之一.
这是它的工作原理.
我们看到OrderByDescending被声明为:
static IOrderedEnumerable<T> OrderByDescending<T,K>( this IEnumerable<T> sequence,Func<T,K> keyExtractor)
OrderByDescending(dictionary,o=>o.Value.Frequency)
但我们不知道T和K是什么.所以我们首先看一下不是lambda的东西.你的字典实现IEnumerable< keyvaluePair< int,FrequencyOrValue>>所以我们首先说“T可能是keyvaluePair< int,FrequencyOrValue>”.
在这一点上,我们没有别的东西可以从不是lambdas的东西中推断出来,所以我们开始看看lambdas.我们看到我们有一个lambda o => o.Value.Frequency,到目前为止我们已经确定keyExtractor的类型是Func< keyvaluePair< int,FrequencyOrValue>,K>我们仍在寻找K.所以我们假设lambda实际上是:
(keyvaluePair<int,FrequencyOrValue> o)=>{return o.Value.Frequency;}
我们问它是否绑定?是!是的,它确实.我们可以成功地编译这个lambda而没有错误,当我们这样做时,我们看到它的所有返回语句都返回一个int.
因此我们推断出K是int,我们现在对整个事物进行完整的类型分析.
这是一个相当直接的推论;他们可以变得更加怪异.如果此主题特别引起您的兴趣,请参阅我博客上的“类型推断”存档.
http://blogs.msdn.com/ericlippert/archive/tags/Type+Inference/default.aspx
特别是,这里有一段视频,我解释了上面的内容以及其他一些有趣的案例:
http://blogs.msdn.com/ericlippert/archive/2006/11/17/a-face-made-for-email-part-three.aspx
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。