Click here to Skip to main content
15,389,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i convert the following line of code in a singe linq query

What I have tried:

var shopIds = await _business.TArtikelShops.Where(x => x.KArtikel == kArtikel).Select(y => y.KShop).ToListAsync();

var addresseIds = await _business.TInetAdresseShops.Where(x => shopIds.Contains(x.KShop)).Select(y => y.KAdresse).ToListAsync();

var countries = await _business.TAdresses.Where(x => addresseIds.Contains(x.KAdresse)).Select(y => y.Land).Distinct().ToListAsync();

return countries;
Updated 16-Jun-22 21:49pm

1 solution

Should be simple enough:
var shopIds = _business.TArtikelShops.Where(s => s.KArtikel == kArtikel).Select(s => s.KShop);
var addresseIds = _business.TInetAddresseShops.Where(a => shopIds.Contains(a.KShop)).Select(a => a.KAddresse);
var countries = _business.TAddresses.Where(a => addresseIds.Contains(a.KAddresse)).Select(a => a.Land).Distinct();
return await countries.ToListAsync();
Only one query will be issued, on the ToListAsync call.

If you want everthing on a single line of C#, you could inline the three variables into the return line.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900