Question

How would you translate specifically the select part of this linq expression into 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()};

So far i have this:

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

I tried this for the select but i get compilor errors.

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

Would appreciate the help thanks.

Was it helpful?

Solution

This is the exact translation. I have no idea why you need the OrderBy tho, considering you are not using the elements in the Select

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

The same result is obtained by

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

and you save some computational time since you don't have to order the collection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top