Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Convert this to non SQL like LINQ
Message
From
09/10/2014 16:31:43
 
 
To
09/10/2014 10:50:37
John Baird
Coatesville, Pennsylvania, United States
General information
Forum:
ASP.NET
Category:
LINQ
Environment versions
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01609048
Message ID:
01609080
Views:
55
>>Looks promising thanks John.
>
>You realize there's no benefit of one over the other. They all compile to same IL code.... just a matter of preference. and in certain cases with multiple join conditions, it can become a nightmare to use fluent syntax.
>
>example: this is easier to read
>
>  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();
>
>
>
>than this and they both do the same thing.
>
>
>                    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();
Previous
Reply
Map
View

Click here to load this message in the networking platform