gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:24:27 24 4
gpt4 key购买 nike

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

即使 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要求两个集合属于同一类型。您正在尝试使用集合或 Student 来调用它和收集IWebElement .

在调用 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)))

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

关于C#: 'IEnumerable<Student>' 不包含 'Intersect' 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41071702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com