C#: 'IEnumerable<Student>' 不包含 'Intersect' 的定义

C#: 'IEnumerable<Student>' does not contain a definition for 'Intersect'

很久没写一行代码了,如果我问的是一个愚蠢的问题,请耐心等待。

尽管 IntelliSense 在 Names 之后显示了 Intersect 方法,但在尝试比较两个 IEnumerable 时出现以下错误。

我正在尝试将数据库查询的结果与 html 中的有序列表进行比较。

'IEnumerable' does not contain a definition for 'Intersect' and the best extension method overload 'Queryable.Intersect(IQueryable, IEnumerable)' requires a receiver of type 'IQueryable'

namespace Data.Repositories
{
    public class StudentsRepository
    {
        public class Student
        { 
            public string FullName { get; set; }
        }

        public static IEnumerable<Student> GetData(string CardNumber, string Section)
        {
            // FullName varchar(300) in Database
            return CommonFunctions.ExecuteReader1<Student>(QryStudentSectionDetails(CardNumber, Section)).AsQueryable();
        }
    }
}


namespace Tests.ActionsLibrary.StudentPaper
{
    public class StudentActions:TestBase
    {
        bool IsMatch = false;

        // Get Names from DataBase
        IEnumerable<Student> Names = GetData(CardNumber, Section);

        // Get Names from Ordered list in HTML
        IEnumerable<IWebElement> OrderedList = driver.FindElements(By.XPath("//li[@ng-repeat='Names']"));

        if (Names.Count() == OrderedList.Count() && Names.Intersect(OrderedList).Count() == OrderedList.Count()) // The error is shown here.
        { IsMatch = true; }

我想知道我做错了什么。任何帮助将不胜感激。谢谢。

最后的代码如下所示:

    IEnumerable<string> Names = GetData(CardNumber, Section).Select(s => s.FullName);
    IEnumerable<string> OrderedList = driver.FindElements(By.XPath("//li[@ng-repeat='Names']")).Select(i => i.Text);

非常感谢您的帮助。

这是因为 Intersect 要求两个集合的类型相同。您正在尝试使用集合或 StudentIWebElement.

的集合来调用它

在调用 Intersect 或使用不同的方法完成任务之前,请确保您有两个相同类型的集合。

您可以将两个集合投影到可以轻松比较的对象中(例如 IEnumerable<string>):

var studentNames = Names.Select(student => student.Name);
var webElementNames = OrderedList.Select(webElement => webElement.Name);

或者您可以使用 All 这样做:

if(Names.All(student => OrderedList.Any(webElement => webElement.Name == student.Name)))

我不知道你想比较什么属性,所以用有意义的东西替换谓词。