2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Linqを理解する

2
Last updated at Posted at 2025-07-23

Linqとは

LINQ(Language Integrated Query、"リンク"と読みます)は、C# や VB.NET などの .NET 言語に統合されたデータクエリ機能です。SQLのような構文で、コレクションやデータベース、XML、JSONなどに対して簡潔かつ型安全にクエリを記述できます。

1. Selectメソッド

要素を別の形に変換します。(マッピング)

■ Linqを使用しない例

int[] array = new int[] { 1, 2, 3, 4, 5 };

for(int i = 0;i < array.Length; i++)
{  
    array[i] = array[i] + 2;
}  

foreach (var item in array) 
{  
    Console.WriteLine(item); 
} 

// 出力結果   3   4   5   6   7

■ Linqを使用する例

int[] array = new int[] { 1, 2, 3, 4, 5 }; 

array = array.Select(n => n + 2).ToArray(); 

foreach (var item in array) 
{  
    Console.WriteLine(item); 
} 

// 出力結果   3   4   5   6   7

2. Whereメソッド

条件に一致する要素だけを抽出します。(フィルタリング)

■ Linqを使用しない例

const int arraysize = 6;

int[] array = new int[arraysize] { 1, 2, 3, 4, 5, 6 };

int[] array2 = new int[arraysize / 2];

for (int i = 0; i < array.Length; i++)
{
  if (array[i] % 2 == 0)
  {
    array2[i / 2] = array[i];
  }
}

foreach (var item in array2)
{
  Console.WriteLine(item);
}

// 出力結果   2   4   6

■ Linqを使用する例

const int arraysize = 6;

int[] array = new int[arraysize] { 1, 2, 3, 4, 5, 6 };

int[] array2 = new int[arraysize / 2];

array2 = array.Where(n => n % 2 == 0).ToArray();

foreach (var item in array2)
{
  Console.WriteLine(item);
}

// 出力結果   2   4   6

3. Anyメソッド:

指定した条件に1つ以上一致している要素がある場合はtrueを返します。
1つもなかった場合はfalseを返します。if文の中で使用することが多いです。

■ Linqを使用しない例

int[] array = new int[] { 1, 2, 3, 4, 5, 6 };

foreach (var item in array)
{
  if (item == 3)
  {
    Console.WriteLine("3が含まれています");
  }
}
// 出力結果   3が含まれています。

■ Linqを使用する例

int[] array = new int[] { 1, 2, 3, 4, 5, 6 };

if (array.Any(n => n == 3))
{
  Console.WriteLine("3が含まれています。");
}
// 出力結果   3が含まれています。

4. Allメソッド:

指定した条件に全ての要素が一致している場合はtrueを返します。
1つでも一致していない場合はfalseを返します。if文の中で使用することが多いです。

■ Linqを使用しない例

int[] array = new int[] { 3, 3, 3, 3 };

bool flg3 = true;

foreach (var item in array)
{
  if (item == 3)
  { flg3 = true; }
  else
  {
    flg3 = false; break;
  }
}

if (flg3)
{
  Console.WriteLine("全ての要素が3です。");
} 
// 出力結果 全ての要素が3です。

■ Linqを使用する例

int[] array = new int[] { 3, 3, 3  3 };

bool flg3 = true;

if (array.All(n => n == 3))
{
  Console.WriteLine("全ての要素が3です。");
}
// 出力結果 全ての要素が3です。

5. Maxメソッド:

1行で最大値を出力することができます。

■ Linqを使用しない例

int[] array = new int[] { 1, 2, 5, 4, 3 };

int max = int.MinValue;

foreach (var item in array)
{
  if (item > max)
  {
    max = item;
  }
}

Console.WriteLine(max); 
// 出力結果  5 

■ Linqを使用する例

int[] array = new int[] { 1, 2, 5, 4, 3 };

Console.WriteLine(array.Max());
// 出力結果  5 

6. Minメソッド:

最小値を求める時はMinメソッドを使用します。
Maxメソッドと同様の使い方です。

■ Linqを使用する例

int[] array = new int[] { 1, 2, 5, 4, 3 }; 
Console.WriteLine(array.Min());
// 出力結果 1 

7. Averageメソッド:

配列の平均を求めることができます。

■ Linqを使用しない例

int[] array = new int[] { 1, 2, 3, 4, 5 };

int sum = 0;

foreach (var item in array)
{
  sum += item;
}

double average = (double)sum / array.Count();

Console.WriteLine(average);
// 出力結果 3

■ Linqを使用する例

int[] array = new int[] { 1, 2, 3, 4, 5 };

Console.WriteLine(array.Average());
// 出力結果 3 

8. Sumメソッド:

配列などの要素の合計値を求めることができます。
使い方は今まで解説したメソッドと同じです。

■ Linqを使用する例

int[] array = new int[] { 1, 2, 5, 4, 3 };

Console.WriteLine(array.Sum());
// 出力結果 15 

参考. Linqを理解すれば、C#プログラミングの世界が変わる 第1版: C#プログラミングを学びたい方、Linqを知りたい方必見! (プログラミング、C#、Linq) . Kindle 版.

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?