若要检查两个序列是否相等,请使用SequenceEqual()
方法。
首先,设置顺序。
Employee emp1 = new Employee { EmployeeRank = 4, EmpName = "Amit", EmpMarks = 90 }; Employee emp2 = new Employee { EmployeeRank = 5, EmpName = "Raman", EmpMarks = 95 }; List<Employee> employee1 = new List<Employee> { emp1, emp2 }; List<Employee> employee2 = new List<Employee> { emp1, emp2 };
现在,查找序列是否相等。
employee1.AsQueryable().SequenceEqual(employee2);
这是显示结果的示例。
using System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { Employee emp1 = new Employee { EmployeeRank = 4, EmpName = "Amit", EmpMarks = 90 }; Employee emp2 = new Employee { EmployeeRank = 5, EmpName = "Raman", EmpMarks = 95 }; List<Employee> employee1 = new List<Employee> { emp1, emp2 }; List<Employee> employee2 = new List<Employee> { emp1, emp2 }; bool res = employee1.AsQueryable().SequenceEqual(employee2); Console.WriteLine("Lists are equal? = "+res); } } public class Employee { public int EmployeeRank { get; set; } public string EmpName { get; set; } public int EmpMarks { get; set; } }
输出结果
Lists are equal? = True