簡體   English   中英

如何比較.NET中的兩個列表

[英]How to compare two lists in .NET

List<Employee> emplist = new List<Employee>();
emplist.Add(new Employee { Name = "Emp_1", BasicSalary = 1000, Id = Guid.NewGuid(), HRA = 100, DA = 10, TotalSalary = 1110 });
emplist.Add(new Employee { Name = "Emp_2", BasicSalary = 1000 * 2, Id = Guid.NewGuid(), HRA = 200, DA = 20, TotalSalary = 2220 });
emplist.Add(new Employee { Name = "Emp_3", BasicSalary = 1000 * 3, Id = Guid.NewGuid(), HRA = 300, DA = 30, TotalSalary = 3330 });

var result = empRep.CallSupportFindAll();

// CollectionAssert.AreEqual(emplist, result);
Assert.AreEqual(emplist, result);
var r1 = result[0];
Assert.AreEqual(r1.Name, emplist[0].Name);
Assert.AreEqual(r1.TotalSalary, emplist[0].TotalSalary);
Assert.AreEqual(r1.BasicSalary, emplist[0].BasicSalary);

我想比較兩個列表emplistresult Assert.AreEqual(r1.Name, emplist[0].Name); 工作,但如果我們有成千上萬的記錄,那么我需要寫成千上萬的行。 所以請回答-對於比較兩個列表的一個行代碼...在此先感謝

如果我理解正確,您僅擁有List<Employee>兩個實例,並且您想斷言它們是相等的。 也就是說,它們具有相同數量的Employee實例,並且順序相同,並且屬性相同。

如果是這樣,則與FakeItEasy無關。 您只需要執行斷言的方法。 我個人將使用Fluent Assertions做到這一點。

result.Should().BeEquivalentTo(emplist);

您可以使用HashSet <>比較兩個列表。

首先,定義一個這樣的均衡器

public class EmployeeComparer : IEqualityComparer<Employee>
    {
        public bool Equals(Employee x, Employee y)
        {
            if (x == null && y == null)
                return true;
            if (x == null || y == null)
                return false;

            //You can implement the equal method as you like. For instance, you may compare by name 
            if (x.Id == y.Id)
                return true;
            return false;
        }

        public int GetHashCode(Employee employee)
        {
            return employee.Id.GetHashCode();
        }
    }

接下來,根據方法的輸入和結果創建2個哈希集

var equaliser = new EmployeeComparer();
HashSet<Employee> inputHashset = new HashSet<Employee>(emplist ,equaliser );
  HashSet<Employee> resultHashset = new HashSet<Employee>(result,equaliser);

最后,聲明兩組的相等性。 這意味着,而不是

 Assert.AreEqual(emplist, result);

 Assert.IsTrue(inputHashset.SetEquals(resultHashset));
  public bool compareTwolist<T>(List<T> lst1,List<T> lst2)
    {

        bool bresult = false;
        if (lst1.GetType() != lst2.GetType())
        {
            return false;
        }

        //if any of the list is null, return false
        if ((lst1 == null && lst2 != null) || (lst2 == null && lst1 != null))
            return false;

        //if count don't match between 2 lists, then return false
        if(lst1.Count != lst2.Count)
            return false;

       foreach (T item in lst1)
       {
           T obj1 = item;
           T obj2 = lst2.ElementAt(lst1.IndexOf(item));
           Type type = typeof(T);

           foreach (System.Reflection.PropertyInfo property in type.GetProperties())
           {
               string obj1Value = string.Empty;
               string obj2Value = string.Empty;

               if (type.GetProperty(property.Name).GetValue(obj1) != null)
                   obj1Value = type.GetProperty(property.Name).GetValue(obj1).ToString();

               if (type.GetProperty(property.Name).GetValue(obj2) != null)
                   obj2Value = type.GetProperty(property.Name).GetValue(obj2).ToString();

               //if any of the property value inside an object in the list didnt match, return false
               if (obj1Value.Trim() != obj2Value.Trim())
               {
                   bresult = false;
                   break;
               }
           }

       }
         return bresult;

 }

是的,我通過創建用戶定義函數並傳遞兩個我們要比較的列表得到了一個解決方案。

只是檢查var a是否為真

var a = compareTwolist(empwithoutid,resultwithoutid); Assert.IsTrue(a);

其他答案中建議的比較方法要求您對所有對象實施均等。 從維護的角度來看,這不能很好地擴展。 同樣在某些測試中,僅要比較字段的子集。 這意味着要實施的多個比較或等於方法。 另一種方法是stateprinter,它將狀態轉儲到字符串中以進行比較。 它甚至可以為您編寫和重寫斷言。 https://github.com/kbilsted/StatePrinter/blob/master/doc/AutomatingUnitTesting.md的自動化和更多信息https://github.com/kbilsted/StatePrinter/blob/master/doc/TheProblemsWithTraditionalUnitTesting.md為您現在面對的有關單元測試問題的深入討論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM