2016-05-12 6 views
-3

クラスCategoryの2つのリストの違いを知る必要があります。2つのリストを比較する "List <Category>"カテゴリは私のクラスです

Categoryクラスには、これらのプロパティがあります。

public class Category 
{ 
    public int Id { get; set; } 

    public string Title { get; set; } 

    public bool IsQuantitative 
    { 
     get { return Products.Any(x => x.IsMultiPart); } 
    } 

    public List<Product> Products { get; set; } 
    public string Image { get; set; } 
    public string Description { get; set; } 
} 
+4

"違い" は広範な用語です。 *具体的な違いは何ですか?アイテムの順序?アイテム数は?各オブジェクトの特定のプロパティの値は?他に何か? –

+0

2つのリストの違いをどう定義するのですか?どのように 'Category'の2つのインスタンスの違いを定義しますか?あなたや他の誰かが実際にそれをやる前にあなたが何をしたいのかを定義しなければなりません。 – David

+0

リスト項目disの値が一致する場合は、その特定の項目を指摘する必要があります。それは意味がありますか –

答えて

0

私は最初のIDで二つのリストを並べ替えます。 これを実行してオブジェクトを比較します。 これは良いオブジェクト比較子である:ここで少し例 https://www.nuget.org/packages/CompareNETObjects/

  //Here you set the config like you want to have it compared 
      ComparisonConfig comparisonConfig = new ComparisonConfig() 
      { 
       CompareChildren = true, 
       CompareFields = true, 
       CompareReadOnly = true, 
       CompareProperties = true, 
       MaxDifferences = 1, 
       MaxByteArrayDifferences = 1 
      }; 

      CompareLogic comparer = new CompareLogic() { Config = comparisonConfig }; 

      list1 = list1.OrderBy(x => x.Id).ToList(); 
      list2 = list2.OrderBy(x => x.Id).ToList(); 


      for (int i =0;i> list1.count;i++) 
      { 
       //Here you get a bool if the two Objects are Equal 
       bool areEqual = comparer.Compare(list1[i], list2[i]).AreEqual; 

       //Here you get a List of Differences Objects. It contains Values like "expected and "actual" etc. 
       var differences = comparer.Compare(list1[i], list2[i]).Differences; 

       //Here you handle Differences etc. 
      } 
+0

この回答を改善するには、簡単な例を示します –

0

この

VAR firstNotSecond = list1.Except(LIST2).ToList()を試してみてください。

var secondNotFirst = list2.Except(list1).ToList();

link

関連する問題