40
28

More than 5 years have passed since last update.

# LINQの集合演算

Last updated at Posted at 2012-12-26

ListやDictonaryなどのIEnumerableインタフェイスを実装しているクラスに対して、LINQの集合演算が使えて便利です。

# 一意な要素の集合

Distinct.cs
``````var list = new string[] { "Ada", "ALGOL", "C#", "COBOL", "Ada", "Java"};
var distinctList = list.Distinct<string>();
``````

# 和集合

Union.cs
``````var list = new string[] { "Ada", "ALGOL", "C#", "COBOL", "Java" };
var list2 = new string[] { "BASIC", "Objective-C", "Ruby", "Python" };

var unionList = list.Union<string>(list2).Where(param => param.Contains("O"));
``````

# 積集合

Intersect.cs
``````var list = new string[] { "Ada", "ALGOL", "C#", "COBOL", "Java" };
var list2 = new string[] { "C#", "Java", "Ruby", "Python" };

var intersectList = list.Intersect<string>(list2);
``````

# 差集合

Distinct.cs
``````var list = new string[] { "Ada", "ALGOL", "C#", "COBOL", "Java" };
var list2 = new string[] { "C#", "Java", "Ruby", "Python" };

var exceptList = list.Except<string>(list2);
``````

# 排他的論理和

Xor.cs
``````var list = new string[] { "Ada", "ALGOL", "C#", "COBOL", "Java" };
var list2 = new string[] { "C#", "Java", "Ruby", "Python" };

var intersect = list.Intersect(list2); //積集合
var union = list.Union(list2); //和集合
var xor = union.Except(intersect); //和集合と積集合の差集合＝排他的論理和
``````
40
28
3

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
40
28