2008-09-05 16 views

答えて

3

Deeno

お楽しみください:

var foos = new List<Foo> { 
    new Foo{Bar = 1,Lum = 1}, 
    new Foo{Bar = 1,Lum = 2}, 
    new Foo{Bar = 2,Lum = 3}, 
}; 

// Using language integrated queries: 

var q = from foo in foos 
     group foo by foo.Bar into groupedFoos 
     let lums = from fooGroup in groupedFoos 
        select fooGroup.Lum 
     select new { Bar = groupedFoos.Key, Lums = lums }; 

// Using lambdas 

var q = foos.GroupBy(x => x.Bar). 
      Select(y => new {Bar = y.Key, Lums = y.Select(z => z.Lum)}); 


foreach (var group in q) 
{ 
    Console.WriteLine("Lums for Bar#" + group.Bar); 
    foreach (var lum in group.Lums) 
    { 
     Console.WriteLine(lum); 
    } 
} 

をLINQの詳細については、 `クエリでG`行を選択101 LINQ Samples

4
var q = from x in list 
     group x by x.Bar into g 
     select g; 

foreach (var group in q) 
{ 
    Console.WriteLine("Group " + group.Key); 
    foreach (var item in group) 
    { 
     Console.WriteLine(item.Bar); 
    } 
} 
+0

を読んで実際には必要ありません。 'xからリストグループxのx.Bar'だけ書くことができます。 – Aaron

関連する問題