c# - Entity Framework: best practice to filter data inside multiple "one-to-many" relationships -


in scenario:

public class software {     [key]     public int id { get; set; }      // properties      public virtual icollection<release> releases { get; set; } }  public class release {     [key]     public int id {get; set;}      public int version {get; set;}      public virtual software software {get; set;}     public virtual icollection<report> reports{get; set;} }  public class report {      [key]     public int id {get; set;}      // properties      public virtual release release {get; set;} } 

if have filter report software.id , release.version it's better use query:

from report in context.reports report.release.version == filterversion &&        report.release.software.id == filterid select report 

or use query instead?

from software in context.softwares software.id == filterid      release in software.releases      release.version == filterversion           report in release.reports           select report 

it's better "filter first" on data or use entity framework navigation properties?

of course it's impossible tell query performs best. depends on indexes , numbers of records in each table, both of have huge impact on execution plan.

but think in case query shape turns out identical both alternatives. example, both report.release.version , release.reports generate inner join. , predicates generated identically.

so boils down personal preferences.

i prefer structure queries ...

  • from entity want in result
  • where filters
  • select

... because easy add/remove predicates later.

so be:

from report in context.reports report.release.version == filterversion &&        report.release.software.id == filterid select report 

in alternative ...

from software in context.softwares software.id == filterid      release in software.releases      release.version == filterversion           report in release.reports           select report 

... predicates "all on place" (well, it's not complex, know mean).

but picture changes if want projection containing data entities:

from report in context.reports report.release.version == filterversion &&        report.release.software.id == filterid select new {     report.name,     report.release.version,     report.release.software.id,     report.release. ... } 

... which, because of dotted references less readable ...

from software in context.softwares software.id == filterid      release in software.releases      release.version == filterversion           report in release.reports select new {     report.name,     release.version,     software.id,     release. ... } 

but again, sql query may still identical.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -