月別: 2019年10月
機能 シーケンス内の要素を降順に並び替えます。 オーバーロード ThenByDescendingには2つのオー …
Start reading ThenByDescending機能 要素を降順に並び替えます。 オーバーロード OrderByDescendingには2つのオーバーロードが …
Start reading OrderByDescending機能 シーケンス内の最初の要素を返します。 ただし、要素が存在しない場合はデフォルト値を返します。 オーバーロ …
Start reading FirstOrDefault機能 条件を満たす要素を返します。 ただし、条件を満たす要素が存在しない場合はデフォルト値を返します。 条件値 …
Start reading SingleOrDefault機能 シーケンス内の指定Indexの要素を返します。 Indexが範囲外の場合は既定値を返します。 オーバーロ …
Start reading ElementAtOrDefault引数 セレクタ サンプル
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
class PetOwner { public string Name { get; set; } public List<String> Pets { get; set; } } public static void SelectManyEx1() { PetOwner[] petOwners = { new PetOwner { Name="Higa, Sidney", Pets = new List<string>{ "Scruffy", "Sam" } }, new PetOwner { Name="Ashkenazi, Ronen", Pets = new List<string>{ "Walker", "Sugar" } }, new PetOwner { Name="Price, Vernette", Pets = new List<string>{ "Scratches", "Diesel" } } }; // Query using SelectMany(). IEnumerable<string> query1 = petOwners.SelectMany(petOwner => petOwner.Pets); Console.WriteLine("Using SelectMany():"); // Only one foreach loop is required to iterate // through the results since it is a // one-dimensional collection. foreach (string pet in query1) { Console.WriteLine(pet); } // This code shows how to use Select() // instead of SelectMany(). IEnumerable<List<String>> query2 = petOwners.Select(petOwner => petOwner.Pets); Console.WriteLine("\nUsing Select():"); // Notice that two foreach loops are required to // iterate through the results // because the query returns a collection of arrays. foreach (List<String> petList in query2) { foreach (string pet in petList) { Console.WriteLine(pet); } Console.WriteLine(); } } /* This code produces the following output: Using SelectMany(): Scruffy Sam Walker Sugar Scratches Diesel Using Select(): Scruffy Sam Walker Sugar Scratches Diesel */ |
参考 …
Start reading SelectMany1-1