简体   繁体   中英

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

It's been long since I write a single line of code so, please, be patient if I am asking a dumb question.

Even though the IntelliSense shows the Intersect method after Names, I get the following error when trying to compare two IEnumerables.

I am trying to compare the result of a database query versus an ordered list in the 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; }

I wonder what I am doing wrong. Any help will be greatly appreciated. Thanks.

At the end the code looks like this:

    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);

Thank you, very much for your help.

That's because Intersect requires both collections to be of the same type. You're trying to call it with a collection or Student and collection of IWebElement .

Make sure you have two collections of the same type before calling Intersect or use a different method to achieve your task.

You can either project both collection into something that can be easily compared (eg IEnumerable<string> ):

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

Or you could probably use All to do so:

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

I don't know what properties you want to compare, so replace the predicate with something that makes sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM