1
Id
がすべて等しいかどうかを確認しているようですが、より簡潔で分かりやすいチェック方法がありますか?オブジェクト一覧のすべてのプロパティをチェックすると等しいです
完全外部結合のようなものとNULLをチェックするものはありますか?
具体的にこの部分:
if (masters1.Count() != masters1.Intersect(masters2).Count())
{
throw new Exception("one IDs did not match two IDs");
}
if (masters2.Count() != masters2.Intersect(masters3).Count())
{
throw new Exception("two IDs did not match three IDs");
}
全例:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
public class Record
{
public string Id;
public string Value;
}
public class Records
{
public Record One;
public Record Two;
public Record Three;
}
public class Program
{
public static void Main()
{
var one = new List<Record>(){new Record(){Id = "one", Value = "1"},new Record(){Id = "two", Value = "2"}};
var two = new List<Record>(){new Record(){Id = "one", Value = "1"},new Record(){Id = "two", Value = "2"}};
var three = new List<Record>(){new Record(){Id = "one", Value = "1"},new Record(){Id = "two", Value = "2"}};
var masters1 = one.Select(x => x.Id);
var masters2 = two.Select(x => x.Id);
var masters3 = three.Select(x => x.Id);
if (masters1.Count() != masters1.Intersect(masters2).Count())
{
throw new Exception("one IDs did not match two IDs");
}
if (masters2.Count() != masters2.Intersect(masters3).Count())
{
throw new Exception("two IDs did not match three IDs");
}
var rv = new List<Records>();
foreach (var master in masters1)
{
var record = new Records()
{
One = one.Single(x => x.Id == master),
Two = two.Single(x => x.Id == master),
Three = three.Single(x => x.Id == master),
};
rv.Add(record);
}
foreach(var x in rv){
Console.WriteLine(x.One.Value);
Console.WriteLine(x.Two.Value);
Console.WriteLine(x.Three.Value);
}
}
}
はこちらをご覧ください:https://dotnetfiddle.net/QtjM7x
masters1とmasters2は文字列のリストなので、カスタムを作る必要はありません。 – LearningJrDev
あなたのケースでは - 正確に:) –
しかし、あなたはRecordクラスのEquals()をオーバーライドすることもできます - そうする必要はありません.Select(x => x.Id)呼び出し –