加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 08:33:29 所属栏目:百科 来源:网络整理
导读:我写了一行代码已经很久了,所以,如果我问一个愚蠢的问题,请耐心等待. 尽管IntelliSense在Names之后显示了Intersect方法,但在尝试比较两个IEnumerables时出现以下错误. 我试图比较数据库查询的结果与html中的有序列表. ‘IEnumerable’ does not contain a de
我写了一行代码已经很久了,所以,如果我问一个愚蠢的问题,请耐心等待.

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

我试图比较数据库查询的结果与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要求两个集合属于同一类型.您正尝试使用集合或学生和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)))

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读