MikeAlike234
MikeAlike234

Reputation: 759

How to add a second where clause to a linq expression

Trying to add a second where clause to a linq expression but it won't register.

var query = _dbSetBookedResource.AsQueryable<Resource>();

var resources = (from Resource in query where Resource.DateFrom == date select Resource)


if(true)
{
resources.Where(b => b.MemberId == currentUserId);
}

For some reason the second where clause won't register.

Upvotes: 0

Views: 211

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500805

For some reason the second where clause won't register.

That's because you're not using the return value anywhere. That's just setting up a query, but then ignoring it. No LINQ methods change the value they're called on - instead they create a new query which has the appropriate filtering, projection etc.

You need:

resources = resources.Where(b => b.MemberId == currentUserId);

Also note that your initial query could be written more simply as:

var resources = query.Where(r => r.DateFrom == date);

Query expressions are overkill when all you want is a simple filter or projection.

Upvotes: 1

Related Questions