LoginSignup
1
0

More than 3 years have passed since last update.

【C#入門】『LINQ』の使い方サンプル【4つ紹介】

Last updated at Posted at 2020-03-28

この記事では、《LINQ》について、
業務を通して学習した内容を、備忘録としてまとめています。

  • 『LINQ』 とは…?
  • 『LINQ』の使い方サンプル【4つ紹介】

こういった内容についてまとめています。

※本記事は、自分で学習したことのまとめ用として書いています。
尚、解説で誤った点があれば、スローして頂ければ喜んでキャッチしますのでお願い致します。

『LINQ』 とは…?

Language Integrated Query の略で、『リンク』と読みます。

公式サイト(MSDN)では、以下のように解説されています。(よく分からないです、、、)

統合言語クエリ (LINQ) は、C# 言語への直接的なクエリ機能の統合に基づくテクノロジのセットの名前です。 これまでは、データに対するクエリは、コンパイル時の型チェックや IntelliSense のサポートがない単純な文字列として表現されてきました。 さらに、SQL データベース、XML ドキュメント、さまざまな Web サービスといったデータ ソースの種類ごとに、異なるクエリ言語を習得する必要がありました。 LINQ では、クエリは、クラス、メソッド、イベントと同様に、ファースト クラスの言語コンストラクトです。

クエリを記述する開発者にとって、最も目立つ LINQ の "統合言語" 部分は、クエリ式です。 クエリ式は、宣言型の "クエリ構文" で記述されます。 クエリ構文を使用することで、フィルター処理、並べ替え、グループ化などのデータ ソースに対する操作を、最小限のコードで実行できます。 同一の基本的なクエリ式のパターンを使用して、SQL データベース、ADO .NET データセット、XML ドキュメントとストリーム、および .NET コレクション内のデータを照会して変換できます。

参照:https://docs.microsoft.com/ja-jp/dotnet/csharp/linq/

要は・・・

『LINQ』は…

コレクション(配列やリスト)に対して処理を行うメソッドを集めたライブラリで、

これまでforeachなどを使って、頑張って実装していた処理を代替してくれます。

サンプル①: Select[]で囲む文字列に変換

program.cs
using System;
using System.Collections.Generic;
using System.Linq;

namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            var number = new List<int>() { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };

            var changedNumber = number    
                                .Select(x => "[" + x + "]");

            foreach (var num in changedNumber)
            {
                Console.WriteLine(num);
            }
        }
    }
}
[10]
[20]
[30]
[40]
[50]
[60]
[70]
[80]
[90]
[100]

サンプル②: Whereで条件指定

program.cs
using System;
using System.Collections.Generic;
using System.Linq;

namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            var number = new List<int>() { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };

            var selectedNumber = number    
                                 .Where(x => x > 50)

            foreach (var num in selectedNumber)
            {
                Console.WriteLine(num);
            }
        }
    }
}
60
70
80
90
100

サンプル③: OrderByで並び順を変える

program.cs
using System;
using System.Collections.Generic;
using System.Linq;

namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            var number = new List<int>() { 20, 10, 40, 30, 60, 50, 80, 70, 100, 90 };

            var orderedNumber = number
                                .OrderBy(x => x) //.OrderByDescending(x => x)

            foreach (var num in orderedNumber)
            {
                Console.WriteLine(num);
            }
        }
    }
}
10
20
30
40
50
60
70
80
90
100

サンプル④: 各種演算

program.cs
using System;
using System.Collections.Generic;
using System.Linq;

namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            var number = new List<int>() { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };

            Console.WriteLine("平均: " + number.Average());

            Console.WriteLine("合計: " + number.Sum());

            Console.WriteLine("最大値: " + number.Max());

            Console.WriteLine("最小値: " + number.Min());

            Console.WriteLine("要素数: " + number.Count());
        }
    }
}
平均: 55
合計: 550
最大値: 100
最小値: 10
要素数: 10
1
0
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
1
0