Entity Framework - How to get filtered Master records along with filtered Detail records? -


my class relationship: classmaster 1----* classsessions
goal: return classmasters have classsessions startdatetime greater current date, along these classsessions (filtered date).

in t-sql i'd this:

select *    classsession    join classmaster on classmaster.classid = classsession.classid    classsession.startdatetime > getdate() 

i need realize same results in entity framework 4 query. thought work, not:

   var classes = ctx.classsessions                  .include("classmasters")                  .where(s => s.startdatetime > datetime.now)                  .select(s => s.classmaster)                  .tolist(); 

this give's me classmasters have classsession startdatetime > current date, classsessions property comes filled all classsessions, including older current date. want classsessions startdatetime > current date.

what doing wrong?

thank much!

var masters = ctx.classmasters          .where(cm => cm.classsessions.any(cs => cs.startdatetime > datetime.now))          .select(cm => new { classmaster = cm, classsessions = cm.classsessions.where(cs => cs.startdatetime > datetime.now)          .tolist().select(e => e.classmaster); 

in case loaded classsession objects somewhere in context, can still this:

var masters = ctx.classmasters          .where(cm => cm.classsessions.any(cs => cs.startdatetime > datetime.now))          .select(cm => new classmasterwithsessionmodel{ classmaster = cm, classsessions = cm.classsessions.where(cs => cs.startdatetime > datetime.now)          .tolist();  public class classmasterwithsessionmodel {     classmaster classmaste { get; set; }     ienumerable<classsession> classsessions { get; set; } } 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -