I have a table layout as shown in this image. One main table (User), and two many-to-many tables (Preference and Location) with junction tables. I have set up the correct relationships in the data model to allow selections from these m-2-m tables....
The report tool I'm writing allows the user to select (from a checklist) any user preferences or user locations. What I'd like to do is choose only the records from the User table where the Preferences OR Locations contains at least one of their selections.
Is this possible with a Linq query? (I did previously do this in SQL, but it seemed easier to write in Linq until I got to this part!)
Many thanks,
EDIT: Visual Studio 2012, Entity Framework 4, SQL Server 2008 R2
from u in Users
where u.Locations.Any(l => l.Name == value) ||
u.Preferences.Any(p => p.Title == value)
select u;
That will generate two EXISTS subqueries. Lambda syntax:
Users.Where(u => u.Locations.Any(l => l.Name == value) ||
u.Preferences.Any(p => p.Title == value));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With