How to filter a list based on another list using Linq?

匿名 (未验证) 提交于 2019-12-03 02:47:02

问题:

I have these two lists, one a list of Venue Objects, one a list of BlockedVenues objects.

I need to filter each item in the listOfAllVenues so that it doesn't contain any venue that is blocked

     IQueryable<Venue> listOfAllVenues = MyDB.Venues;      IQueryable<BlockedVenue> listOfBlockedVenues = Mydb.BlockedVenue;      //I need something to accomplish this please      // var listOfAllVenues_WithoutBlocked_Venues =                             ( Select All venues from listOfAllVenues                              where listOfAllVenues.ID is NOT in                              listOfBlockedVenues.VenueID) 

Please note that yes both list types are different, but listOfAllVenues has an int ID field, and listOfBlockedVenues has a VenueID int field, i need to use these two

Many Thanks

回答1:

Try this:

var filtered = listOfAllVenuses                    .Where(x=>!listOfBlockedVenues.Any(y=>y.VenueId == x.Id)); 

It will get all Venues where Id is not in blockedVenues list



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!