4

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

Toubi
  • 2,469
  • 10
  • 33
  • 49

3 Answers3

5

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))
Dassina
  • 162
  • 9
  • Thanks @Dassina, 2nd worked for me. Can you please also guide how I can change it to "not in" ? Much appreciate your help – Toubi Jan 09 '14 at 00:21
  • Use Except. Reference http://stackoverflow.com/questions/5640259/linq-select-where-object-does-not-contain-items-from-list – Dassina Jan 09 '14 at 00:25
  • Alternatively selectAll.where(x => !idList.contains(x.id)) Reference: http://stackoverflow.com/questions/15574952/lambda-expression-for-not-in – Dassina Jan 09 '14 at 00:28
3

It seems you need Contains

var AllNotifications = _notificationRepos.Where(n => n.ExpiresAt > DateTime.UtcNow 
                               && UserNotificationIds.Contains(n.Id)).ToList();
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • 2
    +1. Note that original sample probably will not compile as [All](http://msdn.microsoft.com/en-us/library/bb548541%28v=vs.110%29.aspx) returns `bool`. – Alexei Levenkov Jan 08 '14 at 23:59
0

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

Community
  • 1
  • 1
Venkatesh Muniyandi
  • 5,132
  • 2
  • 37
  • 40