Home > Blockchain >  Query Expression Join context with primitive list
Query Expression Join context with primitive list

Time:05-17

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

CodePudding user response:

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));
  • Related