score:1
Accepted answer
var query2 = _context.users
.where(u => u.username == username)
.join(_context.userroles, u => u.userid, ur => ur.userid, (u,ur) => ur.role)
.toarray();
score:1
var userswiththeiruserroles = dbcontext.users
// keep only the users with a specific username
.where(user => user.username == username)
// fetch some properties of the remaining users and their userroles
.groupjoin(dbcontext.userroles,
user => user.id, // from each user take the id
userrole => userrole.userid, // from each userrole take the foreign key
// parameter resultselector: take each user, with its zero or more userroles
// to make one new:
(user, userrolesofthisuser) => new
{
// select only the user parameters that you plan to use:
id = user.id,
address = user.address,
...
// select the zero or more user roles of this user
userroles = userrolesofthisuser.select(userrole => new
{
// select only the properties that you plan to use
id = userrole.id,
description = userrole.description,
...
// not needed, you've already got the value:
// userid = userrole.userid,
})
.tolist(),
});
Source: stackoverflow.com
Related Query
- How to retrieve multiple column values with same foreign key id using Linq method Syntax in C#?
- How to query values associated with the foreign key using linq and display in listview
- Get data of multiple column with same foreign key in C# LINQ
- Find foreign key matching multiple row values using linq
- How to Filter Datatable with multiple column values using C#
- How to convert Linq expression with multiple joins into method syntax OR retrieve select index?
- How to sum values for multiple columns in DataTable using Groupby with Linq
- Using linq to merge multiple XML files with the same structure and removing duplicates based on a key
- How to concatenate all child elements with same names values using LINQ to XML
- asp.net mvc 3 EF HTML MultiSelect - How to retrieve multiple values using LINQ
- How to set multiple property values with same name and counter value using reflection?
- How to retrieve a single record using a foreign key column in LINQ?
- How to Update multiple tables with foreign keys using LINQ expression ASP.NET MVC Entity Framework
- How to select multiple values from a Dictionary using Linq as simple as possible
- How to retrieve last 5 records using LINQ method or query expression in C#
- Using Linq to do a Contains with multiple values
- How to remove an element from an xml using Xdocument when we have multiple elements with same name but different attributes
- How do I use Linq ToDictionary to return a dictionary with multiple values in the dictionary items?
- LINQ method How to SelectMany with additional column from Parent class
- LINQ: How to do JOIN using the linq extension method style on multiple fields?
- How to sort datatable with multi column using LINQ
- how to create a pivot table with dynamic column using linq tree expression
- Compare items in different dictionaries with same key using LINQ
- Selecting rows with distinct column values using LINQ
- how to delete multiple rows of data with linq to EF using DbContext
- Problems trying to use GroupBy with multiple properties using the LINQ Method Syntax
- How to group only subsequent items with the same property using linq
- How to get the key when using linq Max() method
- How to get Multiple Result Set in Entity Framework using Linq with C#?
- how to concat two column with hibernate queryover using linq
More Query from same tag
- Group a List by two criteria C#
- EF-Code First: Unable to create a constant value of type ''. Only primitive types ('such as Int32, String, and Guid') are supported in this context
- Is it expensive to parse an ExpressionTree?
- Why is my update not persisting?
- Faking IGrouping for LINQ
- How is join+into clause converted to GroupJoin
- Extract Xdocument soap response body into new Xdocument
- Counting occurrences of the first x elements of sublist in all sublists of the jagged list without grouping
- "Only arguments that can be evaluated on the client are supported for the String.Contains method"
- The LINQ expression could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation EF Core 3.1
- Is there an easy way to merge two ordered sequences using LINQ?
- How do I use LINQ to compare one value from its previous value?
- Comparing two lists of Strings and counting the matches, possible performance problem
- Create a LINQ for the List<> to fetch records based on conditional parameters
- Use Linq.Any() inside a Linq.Where() on CosmosDb
- How to connect 2 tables using LINQ ASP.NET
- 2D Array get number of not null rows
- foreach loop is displaying the same image for all posts in mvc view
- IsNull or Coalesce functionality in LINQ?
- Select multiple child columns with the same filter
- Refactor nested loops into a single LINQ Query
- Only primitive types are supported in this context
- c# - using SelectMany to merge multiple lists
- how to get count new message found as per enquiry/ticket with two way conditions
- I want to convert SQL query to LINQ query
- Need message size for each child record
- LINQ- join with subqueries
- How can I make this query work in LINQ to Entities?
- Event delegates disappear after being added in a foreach loop
- Using Linq with Regex to remove empty matches