Bit of Linq method syntax from a newbie here, so I appreciate any insights anyone can pass along. What I need seems simple, but it’s running me around in circles. I have 2 tables and an association (basically a junction table with only 2 fields). I’ve gone through numerous posts on SO, but I’m just not getting it. Frustrating. Ok, using EF4, I have Reports and Roles and a navigation property on Reports called Roles which is the junction table (with ReportId and RoleId fields, both integer fields). I use a UserId to get all associated roles from the Roles table, use those roles to get the associated reports from the junction table and then get the names of the reports from Reports.
I’ve tried LinqPad, but what it produces in lambda method syntax won’t even work when I paste it back into LinqPad.
SQL query:
select Reports.ReportName
from Reports
inner join UserRoles on Reports.ReportId = UserRoles.ReportId
inner join Roles on Roles.RoleId = UserRoles.RoleId
where UserRoles.UserId == userId
Code so far:
Reports.Select(a => a.Roles.Where(t => t.UserRoles.Select(u => u.UserId == userId)));
You can give this a shot, you may also want to filter down your select based on your needs: