'Query Expression Join context with primitive list

I have the following -

IEnumerable<int> vendorIds;

var vendors = (from v in _context.Vendors
               where vendorIds.Any(v.VendorId));

but what I feel this would not be as efficient as joining the Vendors with the list and would like something like this

var vendors = (from v in _context.Vendors
               join vi in vendorIds on v.VendorId == vi);

That syntax however is not valid. I am not quite sure how to do that comparison. Some help would be appreciated



Solution 1:[1]

Your first way should not have any performance impact. For confirmation you can benchmark both ways.

In Linq query syntax we need to use equals for join like :

var vendors = (from v in _context.Vendors
               join vi in vendorIds on v.VendorId equals vi
               select v);

But this can be done in more simply way like below:

var vendors = _context.Vendors.Where(v => vendorIds.Any(vid => vid == v.VendorId));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ehsan Sajjad