2015-11-02 98 views
0

我想用我預期的List聲明結果List。我預計名單看起來像下面的代碼如何聲明包含相同數據的兩個集合

List<GroupOfStuff> expectedResult = new List<GroupOfStuff>(); 
List<Stuff> stuffList = new List<Stuff>(); 
Stuff stuff = new Stuff 
{ 
    DeliveryDays = 3 
}; 
stuffList.Add(stuff); 
GroupOfStuff groupByCountry = new GroupOfStuff 
{ 
    Country = "Australia", 
    StuffDetails = stuffList // List<Stuff> type 
}; 
expectedResult.Add(groupByCountry); 

和我有實際爲List<GroupOfStuff> actual = obj.Result()

我通過this post去。但我又得到另一個GroupOfStuff類包含另一個List成員。所以,shell我去分開斷言類的每個成員或者什麼建議,我是如何聲明它們的。

回答

0

找到我想你可以使用擴展方法Enumerable.SequenceEqual<TSource>()來達到這個目的。小例子如何使用它:

 class Pet 
     { 
      public string Name { get; set; } 
      public int Age { get; set; } 
     } 

     public static void SequenceEqualEx1() 
     { 
      Pet pet1 = new Pet { Name = "Turbo", Age = 2 }; 
      Pet pet2 = new Pet { Name = "Peanut", Age = 8 }; 

      // Create two lists of pets. 
      List<Pet> pets1 = new List<Pet> { pet1, pet2 }; 
      List<Pet> pets2 = new List<Pet> { pet1, pet2 }; 

      bool equal = pets1.SequenceEqual(pets2); 

      Console.WriteLine(
       "The lists {0} equal.", 
       equal ? "are" : "are not"); 
     } 
相關問題