1

您将如何将此 linq 表达式的选择部分具体翻译为 lambda?

var query2 = from method in typeof(double).GetMethods() // integrated method c# reflection
             orderby method.Name
             group method by method.Name into groups
             select new { MethodName = groups.Key, NumberOfOverloads = groups.Count()};

到目前为止,我有这个:

 var methods = typeof(double).GetMethods();
 var query3 = methods.OrderBy(x => x.Name).GroupBy(y => y.Name);

我为选择尝试了这个,但我得到了编译器错误。

 var query3 = methods.OrderBy(x => x.Name).GroupBy(y => y.Name)
 .Select<new { MethodName = groups.Key, NumberOfOverloads = groups.Count()}>();

感谢帮助谢谢。

4

1 回答 1

7

这是准确的翻译。考虑OrderBy到您没有使用Select

var methods = typeof(double).GetMethods()
     .OrderBy(x=>x.Name)
     .GroupBy(x=>x.Name)
     .Select(x=> new { MethodName = x.Key, NumberOfOverloads = x.Count()});

通过以下方式获得相同的结果

var methods = typeof(double).GetMethods()
 .GroupBy(x=>x.Name)
 .Select(x=> new { MethodName = x.Key, NumberOfOverloads = x.Count()});

并且您节省了一些计算时间,因为您不必订购集合。

于 2013-09-03T22:02:50.143 回答