C# 如何使用 Linq 根据另一个列表过滤列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18977863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:46:28  来源:igfitidea点击:

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

c#visual-studio-2010linq

提问by iAteABug_And_iLiked_it

I have these two lists, one a list of VenueObjects, one a list of BlockedVenuesobjects.

我有这两个列表,一个是Venue对象列表,一个是BlockedVenues对象列表。

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

我需要过滤listOfAllVenues 中的每个项目,使其不包含任何被阻止的场地

     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 listOfAllVenueshas an int ID field, and listOfBlockedVenueshas a VenueID int field, i need to use these two

请注意,是的,两种列表类型都不同,但是listOfAllVenues有一个 int ID 字段,而listOfBlockedVenues有一个 VenueID int 字段,我需要使用这两个

Many Thanks

非常感谢

采纳答案by Kamil Budziewski

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

它将获取所有 Id 不在被阻止的场所列表中的场所