score:47
Accepted answer
if i understand your questions correctly, all you need to do is add the .where(m => m.r.u.userid == 1):
var userinrole = db.userprofiles.
join(db.usersinroles, u => u.userid, uir => uir.userid,
(u, uir) => new { u, uir }).
join(db.roles, r => r.uir.roleid, ro => ro.roleid, (r, ro) => new { r, ro })
.where(m => m.r.u.userid == 1)
.select (m => new addusertorole
{
username = m.r.u.username,
rolename = m.ro.rolename
});
hope that helps.
score:4
i was looking for something and i found this post. i post this code that managed many-to-many relationships in case someone needs it.
var userinrole = db.usersinroles.include(u => u.userprofile).include(u => u.roles)
.select (m => new
{
username = u.userprofile.username,
rolename = u.roles.rolename
});
Source: stackoverflow.com
Related Query
- lambda expression join multiple tables with select and where clause
- Multiple Select and Join with LINQ and Lambda
- Expression tree for groupby with where clause and than select
- Linq how to join tables with a where clause on each table and a count
- Using Or and Select in a Where clause to compare all entries with an int, with lambda expressions
- SQL to LINQ - multiple tables left outer join with where clause referring right table
- C# Create lambda expression with Left and right Join with 3 tables
- Selecting multiple columns with linq query and lambda expression
- Equivalence of query and method (lambda) syntax of a Join with Where clause
- Linq-to-Entities: LEFT OUTER JOIN with WHERE clause and projection
- Linq where clause with multiple conditions and null check
- LINQ to SQL and Join two tables with OR clause
- LINQ to SQL join 3 tables and select multiple columns and also using Sum
- C# - Linq optimize code with List and Where clause
- Entity Framework: How to perform left join with EF and LINQ among multiple tables
- LINQ Select with join and optional where
- Using Trim in multiple where clause of linq with Join clause
- Left join multiple tables using lambda expression
- How to convert multiple SQL LEFT JOIN statement with where clause to LINQ
- LINQ Join With Multiple Where Clause
- Join data from multiple tables into SelectList, and precede each dataset with a caption in MVC
- LINQ Group by on multiple tables with nested group by with join and aggregate
- Select multiple items across multiple tables with join
- C# LINQ join 2 tables with where clause
- Complex joins in Linq with multiple tables and LEFT OUTER JOIN
- How to join multiple tables to one table with lambda expression?
- Join two tables with one to many relationship and pick latest from multiple side
- SQL to LINQ conversion with inner join and where filters on different tables
- Unable to execute LINQ Where clause with generic lambda expression
- LINQ - Left Outer Join with multiple parameters in Where clause
More Query from same tag
- LINQ to Entities / LINQ to SQL: switching from server (queryable) to client (enumerable) in the middle of a query comprehension?
- LINQ to Entity Framwork: return sorted list of related rows
- Method return ViewModel from 2 tables using join and where clause
- Download Excel File with WebAPI/MVC
- Convert SQL to LINQ: select the result into model
- Linq distinct equivalent
- Spliting object using reflection
- How do I handle datetime with no day in a linq comparison?
- Linq select statement not working after grouping
- How would you refactor this LINQ code?
- How to write extension method on ef
- Allow Nulls into a List<DateTime> but exclude nulls in Min.Date()
- c# LINQ: Filter List of Files based on File Size
- Update XElement value in XDocument?
- Get 3 Random elements and 1 preset element from HashSet
- Calling stored procedure from Entity Framework in C#
- Populating child collection property with LINQ sub-query
- Reuse Linq result by adding it in a datatable or dataset
- LINQ to SQL Failing to Insert to the Database
- Not able to filter null value for integer column in DataTable
- LINQ grouping by and creating a formula based on the returned results
- How do I order the elements in a group by linq query, and pick the first?
- Selecting all objects from a list that return true on a member function, as a list?
- Linq custom comparer for contains?
- Ungroup list items using linq c#
- How to build custom PLINQO query by multiple id's?
- Basic LINQ to XML Question
- Not being able to get the correct result
- Datatable null to empty string
- Does the way you write sql queries affect performance?