LoginSignup
13
14

More than 5 years have passed since last update.

LINQメモ

Last updated at Posted at 2014-03-27

たまにLINQのどの操作が何なのか混乱するのでメモ。

Program.cs
//ランダムの生成
var rand = new Random();

//0~99の乱数を100個集めたIEnumerable<int>を作る
var list = Enumerable.Range(0, 100).Select(x => rand.Next(100));

//それを結合して文字列として出力する
Console.WriteLine(string.Join(" ", list));


//標準入力から受け取ったものを' 'で分割、intに変換して10未満のものだけ、数が小さいものから選びList<int>にする
var arr = Console.ReadLine().Split(' ').Select(int.Parse).Where(x => x < 10).OrderBy(x => x).ToList();

//1個ずつ出力する
arr.ForEach(x => Console.Write(string.Format("{0} ", x)));

//最後の改行
Console.WriteLine();

Whereはフィルタ
Selectは射影
OrderByは選択する順番

13
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
14