Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 925
frankztein Avatar asked Dec 10 '16 02:12

frankztein


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

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 (e.g. 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.

like image 73
MarcinJuraszek Avatar answered Sep 26 '22 00:09

MarcinJuraszek