Linq List Comparison and Exception

I have two lists IQueryable<>. Let's say that one is filled with class objects Bottle, and the other is of type Cup.

Both classes have a guid called DrinkID.

How can I take my list Bottleand find all the elements in the list Cupwith DrinkIDwhich is NOT in the list Bottle?

I am looking for something like this:

var bottles = _context.AllBottles;
var cups = _context.AllCups.Where(cup => cup.DrinkID not in bottles.DrinkID);

I am using Linq for Entities.

+3
source share
5 answers
cups.Where(c => !bottles.Any(b => c.DrinkID == b.DrinkID) )
+3
source
var bottles = //....
var cups = // ....

bottleLookup = bottles.ToLookup(b => b.DrinkId);
var cupsNotAvailable = cups.Where(c => !bottleLookup.ContainsKey(c.DrinkId);
+4
source

IHasDrinkID ( ), .

, "IEqualityComparer" ( DrinkComparer).

var bottleless =  Cups.Except(Bottles, new DrinkComparer());
+3

, Entity , .

, Bottles Cups, Bottle Cups. , , , .

, : Cups (), Cup, . , Flask.

EF- ( ). , , Bottles Bottles, Bottles Flasks ( ), Cups .

+1
source
// pull out the ids from the bottles first to prevent re-evaluating for every comparison
var bottle_drink_ids = BottleList.Select(b => b.DrinkID).ToList();
var cups_not_in_bottles_list = CupList.Where(c => !bottle_drink_ids.Contains(c.DrinkID));
0
source

Source: https://habr.com/ru/post/1755386/


All Articles