> var query = (from c in _dbc.Checklist.Where(w => w.FK_TaxList == taxKey && w.PK_Checklist == clKey) > from cld in _dbc.ChecklistData.Where(w => w.FK_Checklist == c.PK_Checklist) > from tx in _dbc.Taxonomy.Where(w => w.FK_Thing == cld.FK_Thing) > from t in _dbc.Thing.Where(w => w.PK_Thing == tx.FK_Thing) > from g in _dbc.Genus.Where(w => w.PK_Genus == tx.FK_Genus) > from sp in _dbc.Species.Where(w => w.PK_Species == tx.FK_Species) > from sb in _dbc.SubSpecies.Where(sb => sb.PK_SubSpecies == tx.FK_SubSpecies).DefaultIfEmpty() > select new { t.PK_Thing, CommonName = t.Name, Genus = g, Species = sp, SubSpecies = sb }).ToList(); > >>
> var query = (_dbc.Checklist.Where(w => w.FK_TaxList == taxKey && w.PK_Checklist == clKey) > .SelectMany(c => _dbc.ChecklistData.Where(w => w.FK_Checklist == c.PK_Checklist), > (c, cld) => new {c, cld}) > .SelectMany(@t1 => _dbc.Taxonomy.Where(w => w.FK_Thing == @t1.cld.FK_Thing), > (@t1, tx) => new {@t1, tx}) > .SelectMany(@t1 => _dbc.Thing.Where(w => w.PK_Thing == @t1.tx.FK_Thing), > (@t1, t) => new {@t1, t}) > .SelectMany(@t1 => _dbc.Genus.Where(w => w.PK_Genus == @t1.@t1.tx.FK_Genus), > (@t1, g) => new {@t1, g}) > .SelectMany(@t1 => _dbc.Species.Where(w => w.PK_Species == @t1.@t1.@t1.tx.FK_Species), > (@t1, sp) => new {@t1, sp}) > .SelectMany( > @t1 => > _dbc.SubSpecies.Where(sb => sb.PK_SubSpecies == @t1.@t1.@t1.@t1.tx.FK_SubSpecies) > .DefaultIfEmpty(), > (@t1, sb) => > new > { > @t1.@t1.@t1.t.PK_Thing, > CommonName = @t1.@t1.@t1.t.Name, > Genus = @t1.@t1.g, > Species = @t1.sp, > SubSpecies = sb > })).ToList(); > >It becomes a bit more manageable if you set up your model properly with navigation properties:
var query = _dbc.Checklist.Where(w => w.FK_TaxList == taxKey && w.PK_Checklist == clKey) .SelectMany(checkList => checkList.ChecklistData) .SelectMany(chkListData => chkListData.Thing.Taxonomies) // Add a .Where here if you want to require that the related Genus or Species is not null .Select(taxonomy=> new { PK_Thing = taxonomy.Thing.PK_Thing, CommonName = taxonomy.Thing.CommonName, Genus = taxonomy.Genus, Species = taxonomy.Species, SubSpecies = taxonomy.SubSpecies}) .ToList();