LINQ

LINQとは

LINQ(Language Integrated Query)は、C#でコレクションやデータを検索・加工・集計するための機能です。

従来の for 文や foreach 文を使った処理を、より簡潔に分かりやすく記述できます。

特に以下のような処理で多く利用されます。

  • 条件検索
  • 並び替え
  • 集計
  • データ抽出
  • グループ化
  • SQLのような操作

LINQを使う準備

LINQを使用する場合は、以下の名前空間を追加します。

using System.Linq;

サンプルデータ

List<int> numbers = new List<int>()
{
    10,
    20,
    30,
    40,
    50
};

Where(条件検索)

条件に一致するデータだけを取得します。

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int>()
        {
            10,
            20,
            30,
            40,
            50
        };

        var result = numbers.Where(x => x >= 30);

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

◆実行結果◆

30
40
50

Select(データ変換)

データを別の形に変換します。

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int>()
        {
            10,
            20,
            30
        };

        var result = numbers.Select(x => x * 10);

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

◆実行結果◆

100
200
300

OrderBy(昇順ソート)

小さい順に並び替えます。

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int>()
        {
            50,
            10,
            30,
            20
        };

        var result = numbers.OrderBy(x => x);

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

◆実行結果◆

10
20
30
50

OrderByDescending(降順ソート)

大きい順に並び替えます。

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int>()
        {
            50,
            10,
            30,
            20
        };

        var result = numbers.OrderByDescending(x => x);

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

◆実行結果◆

50
30
20
10

First(最初のデータ取得)

最初のデータを取得します。

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<string> names = new List<string>()
        {
            "田中",
            "佐藤",
            "鈴木"
        };

        string result = names.First();

        Console.WriteLine(result);
    }
}

◆実行結果◆

田中

FirstOrDefault(存在しない場合はnull)

条件に一致しない場合、エラーではなく既定値を返します。

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int>()
        {
            10,
            20,
            30
        };

        int result = numbers.FirstOrDefault(x => x == 999);

        Console.WriteLine(result);
    }
}

◆実行結果◆

0

Any(存在確認)

条件に一致するデータが存在するか確認します。

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int>()
        {
            10,
            20,
            30
        };

        bool result = numbers.Any(x => x == 20);

        Console.WriteLine(result);
    }
}

◆実行結果◆

True

Count(件数取得)

データ件数を取得します。

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int>()
        {
            10,
            20,
            30
        };

        int result = numbers.Count();

        Console.WriteLine(result);
    }
}

◆実行結果◆

3

Sum(合計)

数値の合計を取得します。

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int>()
        {
            10,
            20,
            30
        };

        int result = numbers.Sum();

        Console.WriteLine(result);
    }
}

◆実行結果◆

60

LINQの特徴

LINQを使うことで、コードを短く分かりやすく記述できます。

◆従来の書き方◆

List<int> result = new List<int>();

foreach (var item in numbers)
{
    if (item >= 30)
    {
        result.Add(item);
    }
}

LINQを使った書き方

var result = numbers.Where(x => x >= 30);

LINQでよく使うメソッド一覧

メソッド内容
Where条件検索
Selectデータ変換
OrderBy昇順ソート
OrderByDescending降順ソート
First最初の取得
FirstOrDefault最初または既定値
Any存在確認
Count件数
Sum合計
Max最大値
Min最小値

まとめ

LINQは、C#で非常によく使用される重要な機能です。

特に以下の場面で多く利用されます。

  • List操作
  • DB検索
  • WebAPIデータ処理
  • CSV処理
  • JSON処理

まずは以下を覚えると実務で役立ちます。

  • Where
  • Select
  • OrderBy
  • FirstOrDefault
  • Any