ในการตรวจสอบว่าสองซีเควนซ์เท่ากันหรือไม่ ให้ใช้เมธอด 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