Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading