Toubi
Toubi

Reputation: 2509

implementing "where in" clause in Lambda?

I have a expression which is returning me Ids:

var UserNotificationIds = _notificationBidderRepos.All(u => u.BidderId == BidderId).Select(n =>n.BidderId);

Another structure has Notifications and requirement is to filter notifications for which Id is provided in UserNotificationIds

var AllNotifications = _notificationRepos.All(n => n.ExpiresAt > DateTime.UtcNow).ToList();

I m trying the following code to query all Notifications but not getting how to impement "where in" in my expression.

Please help

Upvotes: 4

Views: 4985

Answers (3)

Venkatesh Muniyandi
Venkatesh Muniyandi

Reputation: 5640

You can use "Contains" or "Join" approach for "where in" filtering. Code sample and detail in the post here

Upvotes: 0

Dassina
Dassina

Reputation: 162

If it is selecting based off of 1 id

selectAll.where(x => x.id == varId)

If you pass in multiple ids then you need to use .Contains().

selectAll.where(x => idList.contains(x.id))

Upvotes: 5

Selman Genç
Selman Genç

Reputation: 101701

It seems you need Contains

var AllNotifications = _notificationRepos.Where(n => n.ExpiresAt > DateTime.UtcNow 
                               && UserNotificationIds.Contains(n.Id)).ToList();

Upvotes: 3

Related Questions