Reputation: 5619
This query has taken a couple days but I am almost there. I have hacked my way through LINQ to entities to come up with the following from reading a lot of tutorials. However this last part I am missing.
I have two basic objects:
public class DashboardNavigation()
{
public int Id {get; set;}
public string Name {get; set;}
.....etc.....
public List<SubNav> SubDashNav {get; set;}
}
public class SubNav()
{
....properties....
}
var query = _mlsDashboardNavRepository.Table
.OrderBy(x => x.Id)
.Select(x => new
{
Id = x.Id,
Name = x.Name,
Description = x.Description,
ImageUrl = x.ImageUrl,
SubDashNav = _mlsSubDashNav.Table
.Where(s => s.DashId == x.Id)
.Select(y => new { Id = y.Id, DashId = y.DashId, Name = y.Name, Description = y.Description, ImageUrl = y.ImageUrl, IsAdvanced = y.IsAdvanced }).ToList()
}).ToList().Select(q => new MLS_DashboardNavigation()
{
Id = q.Id,
Name = q.Name,
Description = q.Description,
ImageUrl = q.ImageUrl,
SubDashNav = ...???
}).ToList();
My Linq projects DashboardNavigation correctly but I can't figure out how to get SubDashNav populated???
Upvotes: 0
Views: 675
Reputation: 1594
You don't need to select twice. I think this does the same thing.
var query = _mlsDashboardNavRepository.Table
.OrderBy(x => x.Id)
.Select(x => new MLS_DashboardNavigation()
{
Id = x.Id,
Name = x.Name,
Description = x.Description,
ImageUrl = x.ImageUrl,
SubDashNav = _mlsSubDashNav.Table
.Where(s => s.DashId == x.Id).ToList()
}).ToList();
And if you are using Entity Framework and navigation properties with DashIs as a foreign key to subNavDash table then you might even be able to do this
var query = _mlsDashboardNavRepository.Table
.OrderBy(x => x.Id)
.Select(x => new MLS_DashboardNavigation()
{
Id = x.Id,
Name = x.Name,
Description = x.Description,
ImageUrl = x.ImageUrl,
SubDashNav = x.SubDashNav
}).ToList();
And if MLS_DashboardNavigation is the same type as _mlsDashboardNavRepository.Table you won't even need to select as that is whats returned anyway.
var query = _mlsDashboardNavRepository.Table
.OrderBy(x => x.Id).ToList()
Upvotes: 1