3
0

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について調べてみた / I looked into Linq (日本語 / 英語)

Last updated at Posted at 2024-12-21

image.png

日本語

こんばんわ!
Advent Calendar 2024に参加してまして、22日目の記事を書いていこうと思います。
題材は「C# Advent Calendar 2024」ということで、Linqについて書いていこうと思います。

LINQ とは?

LINQ (Language Integrated Query) は、C# に統合されたクエリ言語です。SQL のようなクエリ構文を使って、オブジェクトやデータソースを操作できます。LINQ を使うと、コードがシンプルで直感的になり、生産性が向上します。

使用例

LINQ には 2 つの構文があります。

クエリ式構文(Query Syntax) - SQL ライクな書き方
メソッド式構文(Method Syntax) - メソッドチェーンを使った書き方

偶数を取得するコードをそれぞれで書いてみます。

クエリ式構文
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var evenNumbers = from n in numbers
                  where n % 2 == 0
                  select n;

foreach (var number in evenNumbers)
{
    Console.WriteLine(number);
}
メソッド式構文
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var evenNumbers = numbers.Where(n => n % 2 == 0);

foreach (var number in evenNumbers)
{
    Console.WriteLine(number);
}

メソッドには下記様々あります。バリエーション豊かです

操作 説明
Where 条件に一致する要素をフィルタリングする
Select 各要素を変換する
OrderBy 昇順に並べ替える
OrderByDescending 降順に並べ替える
GroupBy グループ化する
Sum 合計値を計算する
Average 平均値を計算する
Count 要素数をカウントする
First 最初の要素を取得する
Any 条件に一致する要素があるか判定する

ここまでです。
読んでいただきありがとうございます

ENGLISH

Good evening!
I am participating in Advent Calendar 2024 and I am going to write an article for the 22nd day.
The subject is “C# Advent Calendar 2024”, so I will write about Linq.

What is LINQ?

LINQ (Language Integrated Query) is a query language integrated into C# that allows you to manipulate objects and data sources using an SQL-like query syntax.

Example usage

LINQ has two syntaxes.

Query Syntax - SQL-like syntax
Method Syntax - a way to write using method chains

Let's try to write a code to get an even number in each syntax.

Query Syntax
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var evenNumbers = from n in numbers
                  where n % 2 == 0
                  select n;.

foreach (var number in evenNumbers)
{
    Console.WriteLine(number);
}
method expression syntax
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var evenNumbers = numbers.Where(n => n % 2 == 0);

foreach (var number in evenNumbers)
{
    Console.WriteLine(number);
}

Methods include the following There are many variations

Operation Description
Select Convert each element to a new one.
Select Convert each element
Sort by ascending order OrderByDescending
Sort in Descending Order GroupBy
GroupBy Group the elements together
Sum Calculate the total value
Average Calculate the average value
Count Count the number of elements
Any Any element that matches the condition
Any Determine if any element matches the condition

That's all for now.
Thank you for reading!

3
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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?