ºÇ½ª¹¹¿·¡§ orika_ex_miyako 2023ǯ06·î05Æü(·î) 16:58:27ÍúÎò
½èÍý¤òÈæ³Ó¤·¡¢ÃÖ¤´¹¤¨¤Æ¤¤¤¤Ê¤¬¤éÍý²ò¤ò¿¼¤á¤Þ¤·¤ç¤¦¡£
¡¡Á°²ó¤Ï¤³¤Á¤é¤Ç¤¹¡£
¡¡¡¡=>¡¡´û¸¤Î½èÍý¤ò¥ê¥Õ¥¡¥¯¥¿¥ê¥ó¥°¤·¤Æ³Ø¤Ö LINQ ³èÍÑÎã¡
¡¡¡¡=>¡¡´û¸¤Î½èÍý¤ò¥ê¥Õ¥¡¥¯¥¿¥ê¥ó¥°¤·¤Æ³Ø¤Ö LINQ ³èÍÑÎã¢
¡¡LINQ ¤Ë¤ÏÈó¾ï¤Ë¿¿ô¤Î¥á¥½¥Ã¥É¤¬ÍÑ°Õ¤µ¤ì¤Æ¤¤¤Þ¤¹¡£
¡¡¤½¤ÎÃ椫¤é¾Ò²ð¤·¤Þ¤¹¡£
¡¡¤³¤Î¥á¥½¥Ã¥É¤Ï¥·¡¼¥±¥ó¥¹¤¬¾¯¤Ê¤¯¤È¤â1¤Ä¤ÎÍ×ÁǤò»ý¤Ä¤«¤É¤¦¤«¤òȽÄꤷ¤Þ¤¹¡£
¡¡¾ò·ï¥á¥½¥Ã¥É¤ò»ØÄꤹ¤ë¤³¤È¤Ç¡¢ÆÃÄê¤Î¾ò·ï¤òËþ¤¿¤¹Í×ÁǤ¬Â¸ºß¤¹¤ë¤«¤É¤¦¤«¤òȽÄꤹ¤ë¤³¤È¤â¤Ç¤¤Þ¤¹¡£
¡¡¤³¤ì¤ÏÎ㤨¤ÐŨ¤Î¥ê¥¹¥È¤¬¶õ¤«¤É¤¦¤«¡¢¤Þ¤¿¤Ï¥ê¥¹¥ÈÆâ¤ËÆÃÄê¤ÎŨ¤¬Â¸ºß¤¹¤ë¤«¤É¤¦¤«¤ò³Îǧ¤¹¤ëºÝ¤Ë»È¤¨¤Þ¤¹¡£
¡¡¤Þ¤º¤Ï½¾Íè¤Î½èÍý¤Ç¤¹¡£
Í×ÁǤ¬Â¸ºß¤¹¤ë¤«¤É¤¦¤«¤ò³Îǧ¤¹¤ëÊýË¡¤È¡¢ÆÃÄê¤Î¾ò·ï¤òËþ¤¿¤·¤Æ¤¤¤ëÍ×ÁǤ¬Â¸ºß¤¹¤ë¤«¤ò³Îǧ¤¹¤ëÊýË¡¤ò¤½¤ì¤¾¤ìÄ󼨤·¤Þ¤¹¡£
¡ãÍ×ÁǤ¬Â¸ºß¤¹¤ë¤«¡ä
List<int> numbers = new List<int> {1, 2, 3, 4, 5}; bool hasAny = numbers.Count > 0;
¡ãÆÃÄê¤Î¾ò·ï¤òËþ¤¿¤·¤Æ¤¤¤ëÍ×ÁǤ¬¡¢¾¯¤Ê¤¯¤Æ¤â£±¤Ä¸ºß¤¹¤ë¤«¡ä
List<int> numbers = new List<int> {1, 2, 3, 4, 5}; bool anyNumberGreaterThanThree = false; foreach (var number in numbers) { if (number > 3) { anyNumberGreaterThanThree = true; break; } }
¡¡¥³¥ì¥¯¥·¥ç¥óÆâ¤Ë 3 ¤è¤ê¤âÂ礤¤Í×ÁǤ¬£±¤Ä¤Ç¤â´Þ¤Þ¤ì¤Æ¤¤¤ë¤«¤É¤¦¤«¤òȽÄꤷ¤Æ¤¤¤Þ¤¹¡£
¡¡¤½¤ì¤Ç¤Ï¤³¤Î½èÍý¤ò LINQ ¤ò»È¤¤¡¢Any ¥á¥½¥Ã¥É¤ËÃÖ¤´¹¤¨¤Æ¤¤¤¤Þ¤¹¡£
List<int> numbers = new List<int> {1, 2, 3, 4, 5}; bool hasAny = numbers.Any();¡¡¡¡//¡¡Í×ÁǤ¬Â¸ºß¤·¤Æ¤¤¤ë¤« bool anyNumberGreaterThanThree = numbers.Any(number => number > 3);
¡¡°ú¿ô¤ò»ØÄꤹ¤ë¤³¤È¤Ç¡¢¥³¥ì¥¯¥·¥ç¥óÆâ¤Î¤¦¤Á¡¢£±¤Ä¤À¤±¤Ç¤â¾ò·ï¤Ë¹çÃפ·¤Æ¤¤¤ë¤Ê¤é¡¢¤È¤¤¤¦¥±¡¼¥¹¤ÇÍøÍѤǤ¤Þ¤¹¡£
¡¡¤³¤Î¥á¥½¥Ã¥É¤Ï¥·¡¼¥±¥ó¥¹¤Î¤¹¤Ù¤Æ¤ÎÍ×ÁǤ¬»ØÄꤷ¤¿¾ò·ï¤òËþ¤¿¤¹¤«¤É¤¦¤«¤òȽÄꤷ¤Þ¤¹¡£
¡¡¤Þ¤º¤Ï½¾Íè¤Î½èÍý¤Ç¤¹¡£
List<int> numbers = new List<int> {1, 2, 3, 4, 5}; bool allNumbersGreaterThanZero = true; foreach (var number in numbers) { if (number <= 0) { allNumbersGreaterThanZero = false; break; } }
¡¡¥³¥ì¥¯¥·¥ç¥óÆâ¤Î¿ô»ú¤¬¡¢¤¹¤Ù¤Æ 0 ¤è¤ê¤â¿¤¤¤«¤òȽÄꤷ¤Æ¤¤¤Þ¤¹¡£
¡¡¤½¤ì¤Ç¤Ï¤³¤Î½èÍý¤ò LINQ ¤ò»È¤¤¡¢All ¥á¥½¥Ã¥É¤ËÃÖ¤´¹¤¨¤Æ¤¤¤¤Þ¤¹¡£
List<int> numbers = new List<int> {1, 2, 3, 4, 5}; bool allNumbersGreaterThanZero = actions.All(number => number <= 0);
¡¡¤³¤ì¤é¤Î¥á¥½¥Ã¥É¤â¡¢LINQ¤Î¾¤Î¥á¥½¥Ã¥É¤ÈÁȤ߹ç¤ï¤»¤Æ¥á¥½¥Ã¥É¥Á¥§¡¼¥ó¤Ç»ÈÍѤ¹¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
£¥³¥ì¥¯¥·¥ç¥ó¤ÎÍ×ÁǤ¬»ý¤Ä»Ò¥³¥ì¥¯¥·¥ç¥ó¤Î¤¹¤Ù¤Æ¤ÎÍ×ÁǤò¥Õ¥é¥Ã¥È¤Ë¤·¤Æ¼è¤ê½Ð¤¹¡¡¡¼SelectMany ¥á¥½¥Ã¥É¡¼
¡¡SelectMany ¥á¥½¥Ã¥É¤Ï¡¢¥·¡¼¥±¥ó¥¹¤ÎÍ×ÁǤ¬»ý¤Ä»Ò¥³¥ì¥¯¥·¥ç¥ó¤Î¤¹¤Ù¤Æ¤ÎÍ×ÁǤò¥Õ¥é¥Ã¥È(ʿó²½)¤Ë¤·¤Æ¼è¤ê½Ð¤·¤Þ¤¹¡£
¡¡¥²¡¼¥àÆâ¤Ç¥¥ã¥é¥¯¥¿¡¼¤Î¥ê¥¹¥È¤ò´ÉÍý¤·¡¢¤½¤ì¤é¤Î¥¥ã¥é¥¯¥¿¡¼¤¬»ý¤Ã¤Æ¤¤¤ë¥¢¥¤¥Æ¥à¤Î¥ê¥¹¥È¤ò°·¤¦¥·¥Á¥å¥¨¡¼¥·¥ç¥ó¤ò¹Í¤¨¤Æ¤ß¤Þ¤·¤ç¤¦¡£
¡¡¥µ¥ó¥×¥ë¥³¡¼¥É¤Î¤¿¤á¡¢°Ê²¼¤Î¤è¤¦¤Ê¥¯¥é¥¹¹½Â¤¤òÁÛÄꤷ¤Þ¤¹¡£
public class Item { public string Name { get; set; } public int Value { get; set; } }
public class Character { public string Name { get; set; } public List<Item> Items { get; set; } }
¡¡LINQ¤ò»ÈÍѤ·¤Ê¤¤¾ì¹ç¡¢Á´¥¥ã¥é¥¯¥¿¡¼¤¬»ý¤Ä¤¹¤Ù¤Æ¤Î¥¢¥¤¥Æ¥à¤ò¥ê¥¹¥È²½¤¹¤ë¤Ë¤Ï¡¢°Ê²¼¤Î¤è¤¦¤Ë½ñ¤¯¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
List<Item> allItems = new List<Item>(); foreach (var character in characters) { foreach (var item in character.Items) { allItems.Add(item); } }
¡¡LINQ¤ò»ÈÍѤ¹¤ë¾ì¹ç¡¢Æ±¤¸¤³¤È¤ò°Ê²¼¤Î¤è¤¦¤Ë½ñ¤¯¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
List<Item> allItems = characters.SelectMany(character => character.Items).ToList();
»²¹Í¥µ¥¤¥È
Qiita @youmts ÍÍ
SelectMany ¤Î»È¤¤Êý
¡¡GroupBy ¥á¥½¥Ã¥É¤Ï¡¢»ØÄꤷ¤¿¥¡¼¤Ë´ð¤Å¤¤¤Æ¥·¡¼¥±¥ó¥¹¤ÎÍ×ÁǤò¥°¥ë¡¼¥×²½¤·¤Þ¤¹¡£
¡¡LINQ¤ò»ÈÍѤ·¤Ê¤¤¾ì¹ç¡¢¥¥ã¥é¥¯¥¿¡¼¤ò»ý¤Ã¤Æ¤¤¤ë¥¢¥¤¥Æ¥à¤Î̾Á°¤Ç¥°¥ë¡¼¥×²½¤¹¤ë¤¿¤á¤Ë¤Ï¡¢¤«¤Ê¤êÊ£»¨¤Ê¥³¡¼¥É¤ò½ñ¤¯¤³¤È¤Ë¤Ê¤ê¤Þ¤¹¡£
Dictionary<string, List<Item>> groupedItems = new Dictionary<string, List<Item>>(); foreach (var character in characters) { foreach (var item in character.Items) { if (!groupedItems.ContainsKey(item.Name)) { groupedItems[item.Name] = new List<Item>(); } groupedItems[item.Name].Add(item); } }
¡¡¤³¤Î¥³¡¼¥É¤Ï¤Þ¤º¡¢¥¡¼¤¬¥¢¥¤¥Æ¥à¤Î̾Á°¡¢Ãͤ¬³ºÅö¤¹¤ë¥¢¥¤¥Æ¥à¤Î¥ê¥¹¥È¤Ç¤¢¤ëDictionary¤òºîÀ®¤·¤Þ¤¹¡£
¼¡¤Ë¡¢³Æ¥¥ã¥é¥¯¥¿¡¼¤Î¥¢¥¤¥Æ¥à¥ê¥¹¥È¤ò¥ë¡¼¥×¤·¡¢³Æ¥¢¥¤¥Æ¥à¤Ë¤Ä¤¤¤Æ¡¢¤½¤Î̾Á°¤¬´û¤ËDictionary¤Î¥¡¼¤È¤·¤Æ¸ºß¤¹¤ë¤«¤É¤¦¤«¤ò¥Á¥§¥Ã¥¯¤·¤Þ¤¹¡£
¸ºß¤·¤Ê¤¤¾ì¹ç¡¢¿·¤·¤¤¥ê¥¹¥È¤òºîÀ®¤·¡¢¤½¤Î̾Á°¤ò¥¡¼¤È¤·¤ÆDictionary¤ËÄɲä·¤Þ¤¹¡£ºÇ¸å¤Ë¡¢¤½¤Î¥¢¥¤¥Æ¥à¤òÂбþ¤¹¤ë¥ê¥¹¥È¤ËÄɲä·¤Þ¤¹¡£
¡¡LINQ¤ò»ÈÍѤ¹¤ë¾ì¹ç¡¢°Ê²¼¤Î¤è¤¦¤Ë½ñ¤¯¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
var groupedItems = characters.SelectMany(character => character.Items) .GroupBy(item => item.Name);
¡¡LINQ¤ò»ÈÍѤ¹¤ë¤È¡¢¥³¡¼¥É¤Ï°ì¸«¤¹¤ë¤È´Ê·é¤Ë¸«¤¨¤Þ¤¹¤¬¡¢Î¢Â¦¤Ç²¿¤¬¹Ô¤ï¤ì¤Æ¤¤¤ë¤Î¤«¤òÍý²ò¤¹¤ë¤Î¤Ï¾¯¤·Æñ¤·¤¯¤Ê¤ê¤Þ¤¹¡£
¤½¤ì¤ËÂФ·¤Æ½¾Íè¤ÎÊýË¡¤Ç¤Ï¡¢¤½¤ÎµÕ¤Ç¡¢¥³¡¼¥É¤Ï¾¯¤·¾éĹ¤Ë¤Ê¤ê¤Þ¤¹¤¬¡¢¤½¤ì¤¾¤ì¤Î¥¹¥Æ¥Ã¥×¤Ç²¿¤¬¹Ô¤ï¤ì¤Æ¤¤¤ë¤Î¤«¤òľ´¶Åª¤ËÍý²ò¤¹¤ë¤³¤È¤¬Íưפˤʤê¤Þ¤¹¡£
¡¡¤½¤Î¤¿¤á¡¢¤ä¤Ï¤ê¡¢Î¾Êý¤Î½èÍý¤ò½ñ¤Èæ¤Ù¤Æ¤¤¤¯¤³¤È¤Ç¡¢Íý²ò¤ò¿¼¤á¤Æ¤¤¤¯³Ø½¬¤Ë¤Ä¤Ê¤¬¤ê¤Þ¤¹¡£
¡¡¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤È¥¸¥§¥Í¥ê¥Ã¥¯¤ò»È¤Ã¤¿LINQ¤Î»ÈÍÑË¡¤Ë¤Ä¤¤¤ÆÀâÌÀ¤·¤Þ¤¹¡£
¡¡¤Þ¤º¡¢°Ê²¼¤Î¤è¤¦¤Ê¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤È¤½¤ì¤ò¼ÂÁõ¤·¤¿£²¤Ä¤Î¥¯¥é¥¹¤ò¹Í¤¨¤Æ¤ß¤Þ¤·¤ç¤¦¡£
public interface IAnimal { string Name { get; set; } string Speak(); }
public class Dog : IAnimal { public string Name { get; set; } public string Speak() { return "Woof!"; } }
public class Cat : IAnimal { public string Name { get; set; } public string Speak() { return "Meow!"; } }
¡¡¤³¤Î¤È¤¡¢°Ê²¼¤Î¤è¤¦¤ËIAnimal¤Î¥ê¥¹¥È¤òºî¤ê¡¢¤½¤ÎÃæ¤ËDog¤ÈCat¤Î¥ª¥Ö¥¸¥§¥¯¥È¤òº®ºß¤µ¤»¤ÆÆþ¤ì¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
List<IAnimal> animals = new List<IAnimal> { new Dog { Name = "Rover" }, new Cat { Name = "Whiskers" }, new Dog { Name = "Spot" } };
¡¡¤³¤ì¤Ï¡¢IAnimal ·¿¤Î List ¤Ç¤¢¤ë¤¿¤á¡¢IAnimal ¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤ò¼ÂÁõ¤·¤Æ¤¤¤ë¥¯¥é¥¹¤Ç¤¢¤ì¤Ð List ¤ËÊÝ»ý¤Ç¤¤ë¤¿¤á¤Ç¤¹¡£
¡¡
¡¡¤½¤Î¤¿¤á¡¢¥¯¥é¥¹¤òÆÃÄꤻ¤º¡¢ÈÆÍÑÀ¤Î¤¢¤ë´ÉÍý¤¬²Äǽ¤Ë¤Ê¤ê¤Þ¤¹¡£
¡¡¤½¤·¤Æ¡¢OfType¥á¥½¥Ã¥É¤ò»È¤¦¤È¡¢¤³¤Î¥ê¥¹¥È¤«¤éÆÃÄê¤Î·¿¤ÎÍ×ÁǤÀ¤±¤òÃê½Ð¤¹¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
¡¡°Ê²¼¤Ï¡¢OfType¤ò»È¤Ã¤ÆDog¤Î¤ß¤òÃê½Ð¤¹¤ëÎã¤Ç¤¹¡£
List<Dog> dogs = animals.OfType<Dog>().ToList();
¡¡¤³¤Î½èÍý¤Ë¤è¤ê¡¢List Æâ¤Ëº®ºß¤·¤Æ¤¤¤ë Animal ¤Î¤¦¤Á¡¢Dog ¤À¤±¤òÃê½Ð¤·¤¿ List ¤òºîÀ®¤¹¤ë¤³¤È¤¬½ÐÍè¤Þ¤¹¡£
¡¡¤Þ¤¿¡¢Select¤ò»È¤Ã¤ÆÆÃÄê¤Î°À¤òÃê½Ð¤¹¤ë¤³¤È¤â¤Ç¤¤Þ¤¹¡£°Ê²¼¤ÎÎã¤Ç¤Ï¡¢Á´¤Æ¤Îưʪ¤Î̾Á°¤òÃê½Ð¤·¤Æ¤¤¤Þ¤¹¡£
List<string> names = animals.Select(animal => animal.Name).ToList();
¡¡¤³¤Î¤è¤¦¤Ë¡¢LINQ¤Ï¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤È¥¸¥§¥Í¥ê¥Ã¥¯¤ò»È¤Ã¤Æ¡¢°Û¤Ê¤ë·¿¤ÎÍ×ÁǤò»ý¤Ä¥ê¥¹¥È¤«¤éÆÃÄê¤Î·¿¤ÎÍ×ÁǤÀ¤±¤òÃê½Ð¤·¤¿¤ê¡¢
ÆÃÄê¤Î°À¤òÃê½Ð¤·¤¿¤ê¤¹¤ë½èÍý¤òÍưפ˽ñ¤¯¤³¤È¤¬¤Ç¤¤Þ¤¹¡£¤³¤ì¤Ë¤è¤ê¡¢¥³¡¼¥É¤ÎºÆÍøÍÑÀ¤È½ÀÆðÀ¤¬¹â¤Þ¤ê¤Þ¤¹¡£
- ¥«¥Æ¥´¥ê¡§
- ¿Ê³Ø/¥¹¥¯¡¼¥ë
- ¥×¥í¥°¥é¥ß¥ó¥°
¥³¥á¥ó¥È¤ò¤«¤¯