Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 543
iAteABug_And_iLiked_it Avatar asked Sep 24 '13 09:09

iAteABug_And_iLiked_it


People also ask

How do I select a query in Linq?

LINQ query syntax always ends with a Select or Group clause. The Select clause is used to shape the data. You can select the whole object as it is or only some properties of it. In the above example, we selected the each resulted string elements.

What is Linq in C# with example?

Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities directly into the C# language. Traditionally, queries against data are expressed as simple strings without type checking at compile time or IntelliSense support.

What is the LINQ equivalent to the SQL IN operator?

An IEnumerable<T>.


1 Answers

Try this:

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

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

like image 144
Kamil Budziewski Avatar answered Sep 19 '22 06:09

Kamil Budziewski