using System;
using System.Linq; // LINQ拡張メソッドの利用に必要。メソッド構文ではなくクエリ構文を使用する場合でも必要となる。
public class Test
{
public static void Main()
{
string[] fruits =
{
"apple",
"cherry",
"melon",
"orange",
"marron",
"mango",
};
// クエリ構文
Console.WriteLine("Query Syntax");
{
// fromからselectまでの一連の式がLINQクエリ式である。
var ret = from x in fruits
where !string.IsNullOrEmpty(x) && x[0] == 'm'
select x;
foreach (var e in ret)
{
Console.WriteLine(e);
}
}
// メソッド構文
Console.WriteLine("Method Syntax");
{
// オブジェクトをフィルタリングする条件(述語)を、ラムダ式で指定する。
var ret = fruits.Where(x => !string.IsNullOrEmpty(x) && x[0] == 'm');
foreach (var e in ret)
{
Console.WriteLine(e);
}
}
}
}