コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【C#】LINQ で使用できる拡張メソッドが増える「ExtraLINQ」紹介

はじめに

「ExtraLINQ」をプロジェクトに追加することで
LINQ で使用できる拡張メソッドが増えます

インストール

f:id:baba_s:20180129175245p:plain

Visual Studio メニューの「ツール>NuGet パッケージ マネージャー>
ソリューションの NuGet パッケージの管理」を選択します

f:id:baba_s:20180129175310p:plain

「参照」タブを選択して、検索欄に「ExtraLINQ」と入力して
表示された「ExtraLINQ」を選択します

f:id:baba_s:20180129175319p:plain

インストールしたいプロジェクトをチェックして「インストール」ボタンを押します

使い方

using ExtraLinq;

「ExtraLINQ」を使用する際は、ソースコードの先頭に上記の using を追加します

拡張メソッド一覧

Chunk

var list = new [] { 1, 2, 3, 4, 5, 6, 7 };
var result = list.Chunk( 3 ).ToArray();

// result = [[1, 2, 3], [4, 5, 6], [7]]

Cycle

var list = new [] { 0, 1 };
var result = list.Cycle().Take( 5 ).ToArray();

// result = [0, 1, 0, 1, 0]

Distinct

var list = new [] 
{ 
    "JavaScript", 
    "Javascript", 
    "javascript" 
};
var result = list
    .Distinct( c => c.ToLower() )
    .ToArray()
;

// result = ["JavaScript"]

Each

var list = new []
{
    "One Ring to rule them all",
    "One Ring to find them",
    "One Ring to bring them all",
    "and in the darkness bind them"
};

list.Each( Console.WriteLine );

// Console output:
//
// One Ring to rule them all
// One Ring to find them
// One Ring to bring them all
// and in the darkness bind them

Flatten

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

var result = list.Flatten().ToArray();

// result = [1, 2, 3, 4, 5, 6]

HasAtLeast

var list = new [] { "Narya", "Nenya", "Vilya" };

list.HasAtLeast( 0 ) // true
list.HasAtLeast( 2 ) // true
list.HasAtLeast( 4 ) // false
var list = new [] { "Narya", "Nenya", "Vilya" };

list.HasAtLeast( 2, c => c.StartsWith( "N" ) ) // true
list.HasAtLeast( 3, c => c.StartsWith( "N" ) ) // false

HasAtMost

var list = new [] { "Narya", "Nenya", "Vilya" };

list.HasAtMost( 2 ) // false
list.HasAtMost( 3 ) // true
list.HasAtMost( 4 ) // true
var list = new [] { "Narya", "Nenya", "Vilya" };

list.HasAtMost( 1, c => c.StartsWith( "N" ) ) // false
list.HasAtMost( 2, c => c.StartsWith( "N" ) ) // true

HasExactly

var list = new [] { "Narya", "Nenya", "Vilya" };

list.HasExactly( 2 ) // false
list.HasExactly( 3 ) // true
list.HasExactly( 4 ) // false
var list = new [] { "Narya", "Nenya", "Vilya" };

list.HasExactly( 1, c => c.StartsWith( "N" ) ) // false
list.HasExactly( 1, c => c.StartsWith( "V" ) ) // true
list.HasExactly( 2, c => c.StartsWith( "N" ) ) // true

Intersperse

var list = new [] { 1, 2, 3, 4, 5 };
var result = list.Intersperse( 0 ).ToArray();

// result = [1, 0, 2, 0, 3, 0, 4, 0, 5]

IsEmpty

new int[0].IsEmpty() // true
new[] { 1, 2, 3 }.IsEmpty() // false

IsNullOrEmpty

(null as int[]).IsNullOrEmpty() // true
new int[0].IsNullOrEmpty() // true
new[] { 1, 2, 3 }.IsNullOrEmpty() // false

JoinedBy

var list = new [] { "The", "One", "Ring" };
var result = list
    .Select( c => c.ToUpper() )
    .JoinedBy(" ")
;

// result = "THE ONE RING"

None

var list = new [] { "Narya", "Nenya", "Vilya" };
var result = list.None( string.IsNullOrWhiteSpace );

// result = true

Partition

var list = new [] { 1, 2, 3, 4, 5 };
var result = list.Partition( c => c % 2 == 0 );

// result.Matches = [2, 4]
// result.Mismatches = [1, 3, 5]

Random

var list = Enumerable.Range( 1, 49 ).ToArray();
var result = list
    .Random( 6 )
    .OrderBy( c => c )
    .ToArray();

// result = [5, 19, 20, 27, 38, 41]

Repeat

var list = new [] { "om", "nom", "nom" };
var result = list.Repeat( 3 ).ToArray();

// result = ["om", "nom", "nom", "om", "nom", "nom", "om", "nom", "nom"]

Shuffle

var list = new [] { "Frodo", "Sam", "Merry", "Pippin" };
var result = list.Shuffle().ToArray();

// result = ["Sam", "Pippin", "Frodo", "Merry"]

TakeSkip

var list = new []
{
    "Frodo",
    "Sam",
    "Merry",
    "Pippin",
    "Aragorn",
    "Legolas",
    "Gimli",
    "Boromir",
    "Gandalf"
};

var result = list.TakeSkip( 1, 1 ).ToArray();

// result = ["Frodo", "Merry", "Aragorn", "Gimli", "Gandalf"]

ToHashSet

var list = "Nasty hobbitses, gollum, gollum!";
var result = list
    .Split( new[] { ' ', ',', '!' }, StringSplitOptions.RemoveEmptyEntries )
    .ToHashSet()
;

// result = ["Nasty", "hobbitses", "gollum"]

WhereNot

var list = new [] { "Narya", "Nenya", "Vilya" };
var startsWithN = value => value.StartsWith( "N" );
var result = list.WhereNot( startsWithN ).Single();

// result = "Vilya"

Without

var list = new [] { "Frodo", "Sam", "Merry", "Pippin" };
var result = list.Without( "Merry", "Pippin" ).ToArray();

// result = ["Frodo", "Sam"]

関連記事