using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
//整数列 collection から偶数列をもとめる
List<int> collection = [0,1,2,3,4,5,6,7,8,9];
List<int> list = [.. from item in collection where item % 2 == 0 select item];
Console.WriteLine(string.Join(", ", list)); // 0, 2, 4, 6, 8
//数列をゼロから作るパターン 要拡張メソッド
List<int> list2 = [.. from x in (0..9).To() where x % 2 == 0 select x];
Console.WriteLine(string.Join(", ", list2));
//拡張メソッドつかわない(ちょっとリスト内包表記ぽくない)
List<int> list3 = [.. from x in Enumerable.Range(0,10) where x % 2 == 0 select x];
Console.WriteLine(string.Join(", ", list3));
//var listF = [.. from x in (0..9).To() where x % 2 == 0 select x]; //There is no target type for the collection expression.
//(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)
List<(int, int)> pairs = [ .. from x in (0..3).To() from y in (0..3).To() select (x, y)];
Console.WriteLine(string.Join(", ", pairs));
List<int> list4 = [.. from item in collection where item > 5 orderby item descending select item];
Console.WriteLine(string.Join(", ", list4)); //9, 8, 7, 6
}
//拡張メソッド
public static class RangeExtensions
public static IEnumerable<int> To(this Range range)
int start = range.Start.Value;
int end = range.End.Value;
return Enumerable.Range(start, end - start);