C# Queryable SequenceEqual() 方法


若要检查两个序列是否相等,可以使用 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

更新于: 2020 年 6 月 23 日

95 次浏览

开启 职业生涯

完成课程并获得认证

入门
广告