4
在this example:這個Linq查詢的等效C#擴展方法是什麼?
public void Linq40()
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var numberGroups =
from n in numbers
group n by n % 5 into g
select new { Remainder = g.Key, Numbers = g };
foreach (var g in numberGroups)
{
Console.WriteLine("Numbers with a remainder of {0} when divided by 5:",
g.Remainder);
foreach (var n in g.Numbers)
{
Console.WriteLine(n);
}
}
}
什麼是純C#相同呢?我得到這個...
var numberGroups = numbers.GroupBy(n => n % 5)...
但into
子句是一個有點神祕,我無法弄清楚如何從Select
得到Key
。
我想你的意思是用「擴展方法」來實現它的方式? – JOBG 2010-11-22 23:03:56