CloudyKooper
CloudyKooper

Reputation: 717

Why am I geting this error: Operator ‘&&’ cannot be applied to operands of type System.Linq.IQueryable?

I’m trying to retrieve data from an entity and populate a viewModel property like this:

viewModel.Enrollments = db.Enrollments.Where(b => b.classDays == "Monday") && (db.Enrollments.Where(b => b.CourseID == courseID);

but I get a operator && cannot be applied to operands of type System.Linq.IQerable<> error. Can you help with a way to find all Monday class with the same ID?

I tried this: viewModel.Enrollments = db.Enrollments.Where(b => b.classDays == "Monday") but I get all Mondays courses but I want to limit them to a specific courseID.

Please help!

Upvotes: 0

Views: 1367

Answers (2)

Bishnu Paudel
Bishnu Paudel

Reputation: 2079

&& Operator should be used with conditions inside where NOT with sets of enrollments (in your case)

Try This:

viewModel.Enrollments = db.Enrollments.Where(b => (b.classDays == "Monday") && (b.CourseID == courseID));

Upvotes: 0

David
David

Reputation: 218867

You need to examine your parentheses. This code won't even compile:

viewModel.Enrollments = db.Enrollments.Where(b => b.classDays == "Monday")
                    && (db.Enrollments.Where(b => b.CourseID == courseID);

In that code you're trying to use && between two calls to .Where(), which return an IQueryable. You probably mean to use && within the .Where() clause:

viewModel.Enrollments = db.Enrollments.Where(b => (b.classDays == "Monday")
                                               && (b.CourseID == courseID));

Or perhaps append a second .Where() clause:

viewModel.Enrollments = db.Enrollments.Where(b => b.classDays == "Monday")
                                      .Where(b => b.CourseID == courseID);

Note that .Where() can be chained indefinitely, essentially resulting in applying each clause in turn in an AND fashion in the resulting query.

Upvotes: 3

Related Questions