まず第一に、私はベテランのプログラマーではないと言わなければなりません。StackOverflowで同様の問題を調べましたが、限られたスキルで実装できる適切な答えが見つからなかったようです。
C#では、オブジェクトの1つ以上のプロパティの値に基づいて、オブジェクトの2つのリストを比較する必要があります。2つの新しいリストを作成したいと思います。1つは左側に存在するオブジェクトですが、右側のリストにはいくつかのプロパティ値が異なるか、まったく存在しません。その逆も同様です。
以前は、1つの値に基づいて2つを比較するだけでよいので、オブジェクトではなく文字列で作業する必要がなかったので、次のようにしました。
(LeftItems and RightItems are Entities)
List<String> leftList = new List<string>();
List<String> rightList = new List<string>();
List<String> leftResultList = new List<string>();
List<String> rightResultList = new List<string>();
List<String> leftResultObjList = new List<string>();
List<String> rightResultObjList = new List<string>();
foreach (item i in leftItems)
{
leftlist.Add(i.value);
}
//same for right
foreach (string i in leftList)
{
if(!rightList.contains(i))
{
leftResultList.Add(i);
}
}
//same for the right list
ここで、複数の値を比較する必要があるため、比較する必要のあるいくつかのプロパティを持つクラスを作成しました。したがって、上記と同じことを行いますが、オブジェクトのプロパティを使用します。
class CompItems
{
string _x;
string _y;
public CompItems(string x, string y)
{
_x = x;
_y = y;
}
}
foreach (item i in leftItems)
{
leftList.Add(new CompItem(i.value1,i.value2));
}
//same for the right list
foreach (CompItem c in leftItems)
{
// Here is where things go wrong
if(one property of object in rightItems equals property of object in leftItems) && some other comparisons
{
resultLeftObjList.Add(c)
}
}
//And the same for the right list