score:1
Accepted answer
Try this:
(from cat in cats
join res in resources on cat.ResId equals res.Id let categoryName = res.Text
join cat1 in cats on cat.ParentId equals cat1.Id into parentJoin
from pj in parentJoin.DefaultIfEmpty() let parentCatResId =pj==null?0: pj.ResId
join res1 in resources on parentCatResId equals res1.Id into resJoin
from res2 in resJoin.DefaultIfEmpty() let parentName = (res2==null?string.Empty:res2.Text)
select new CategoryVM
{
Id = cat.Id,
ResId = cat.ResId,
CatName = categoryName,
ParentId = cat.ParentId,
ParentName = parentName
}).ToList();
score:0
Say you have the following data with your tables
dbo.Categories
ID CategoryNameResID ParentCategory
1 1 NULL
2 2 NULL
3 3 1
4 4 NULL
5 5 4
6 6 4
7 7 4
dbo.Resources
ID Text Lang
1 Standard en-GB
2 Custom en-GB
3 Standard Oversize en-GB
4 Loose en-GB
5 Loose 2F Set en-GB
6 Loose (4” Scale) en-GB
7 Loose (6” Scale) en-GB
The following LINQ statements will output the desired results:
public class CategoryViewModel
{
public int ID { get; set; }
public int CategoryNameResID { get; set; }
public string CategoryName { get; set; }
public int? ParentCategory { get; set; }
public string ParentCategoryName { get; set; }
}
var categories = (from cat in Categories
join res in Resources on cat.CategoryNameResID equals res.ID let categoryName = res.Text
select new CategoryViewModel
{
ID = cat.ID,
CategoryNameResID = cat.CategoryNameResID,
CategoryName = categoryName,
ParentCategory = cat.ParentCategory,
ParentCategoryName = Resources.FirstOrDefault(r => r.ID == cat.ParentCategory).Text
}).ToList();
foreach(var c in categories)
{
Console.WriteLine(c.CategoryName + " - " + c.ParentCategoryName);
}
// Prints
Standard -
Custom -
Standard Oversize - Standard
Loose -
Loose 2F Set - Loose
Loose (4” Scale) - Loose
Loose (6” Scale) - Loose
Source: stackoverflow.com
Related Articles
- LINQ MVC ViewModel: Multiple joins to the same table with optional field
- Convert SQL with multiple joins (with multiple conditions) of the same table to LINQ
- Linq to NHibernate generating multiple joins to the same table
- Multiple outer join using Linq with 2 joins to the same table/object. Got the SQL, need the Linq to Entity
- LINQ to Entities - Multiple Joins on Same Table
- Multiple joins to the same table with the Entity Framework
- Joins with multiple fields on GroupBy table data in LINQ query/method
- Linq Query from Same table with multiple conditions
- multiple joins in LINQ Query with 3 tables on the same column
- LINQ left join with count - multiple table joins Visual Studio 2015 MVC 5 C#
- Linq statement to select optional fields with multiple joins
- Linq to Entity Join table with multiple OR conditions
- convert linq to lambda with multiple joins
- C# - Combine multiple LINQ collections with same properties
- LINQ Filter query with multiple optional parameters
- C# Linq Query with Multiple Joins with an await
- LINQ - Multiple left joins with nullable values
- Linq to Sql with the And operator on the same field
- Doing 2 Left Joins on the same table with one call
- Linq Query With Multiple Joins Not Giving Correct Results
- C# LINQ with XML, cannot extract multiple fields with same name into object
- Not able to access fields with group by and multiple joins with linq
- Use LINQ to concatenate multiple rows list with same value into single row
- Convert sql query with multiple inner joins to LINQ
- How to perform left join in linq having multiple table joins
- Linq and EntityFramework 4 with multiple inner joins with nested subqueries
- Linq Groupby multiple columns with the same name (An anonymous type cannot have multiple properties with the same name)
- Using LINQ with stored procedure that returns multiple instances of the same entity per row
- Linq with Multiple Joins
- Linq multiple join on same table
- Conditionally map a new property in LINQ based on another value
- ls LINQ an ORM (Object Relational Mapper)?
- Difference between LINQ FirstOrDefault vs. Where(...).FirstOrDefault?
- How to write this query in Linq2Sql
- c# combine x amount of items from 2 lists into a 3'rd list
- Entity Framework Many to Many works but Include does not
- Check for nulls in linq statement so an error doesnt occur
- How can you handle this sub-query with LINQ to SQL?
- How find the min element of a nested list and return its siblings, and its parents Ids
- Linq string.Join group values
- Return Rows with Unique and Duplicate values from dataTable using LINQ
- how I can have the following value using Linq
- Should I not use LINQ to objects because Microsoft might change it?
- How do I retrieve values from an XML that (I think) doesn't have standard formatting?
- Linq to XML - set Xelement value depending on if Statement
- Previous non-empty value in a DateTime LINQ join
- Use LINQ to denormailize a list of Objects
- Adding where and sum to lambda expression
- Linq Query Always returns False and Failed to Fetch Data
- How do you avoid Client Evaluation Errors in EF Core when Ordering by a Child property?