c# - How can I include more than one level deep in a LINQ query? -


i have 3 sql tables represented classes , have entity framework 6 join these tables details of exam, test , usertest tables usertest.userid 0 or x.

i have set respository , works simple queries unable join usertest class in linq @ bottom of question.

here's classes:

public class exam {     public int examid { get; set; }     public int subjectid { get; set; }     public string name { get; set; }     public virtual icollection<test> tests { get; set; } }  public class test {     public int testid { get; set; }     public int examid { get; set; }     public string title { get; set; }     public virtual icollection<usertest> usertests { get; set; } }  public class usertest {     public int usertestid { get; set; }     public string userid { get; set; }     public int testid { get; set; }     public int questionscount { get; set; } } 

what have query looks this:

var exams = _examsrepository            .getall()            .where(q => q.subjectid == subjectid)            .include(q => q.tests )            .include(q => q.tests.usertests) // error on line            .tolist(); 

but it's not letting me include usertests in vs2013.

update:

here query first tried:

  var usertests = _usertestsrepository         .getall()         .include(t => t.test)         .include(t => t.test.exam)         .where(t => t.userid == "0" || t.userid == userid); 

this 1 seemed work when looked @ output saw this:

[{"usertestid":2,   "userid":"0",   "testid":12,   "test":{       "testid":12,"examid":1,       "exam":{           "examid":1,"subjectid":1,           "tests":[                {"testid":13,"examid":1,"title":"sample test1",                 "usertests":[                       {"usertestid":3,                        "userid":"0", 

note starts repeat , bring lot more data expected

that's because tests collection , not single object, doesn't have usertests property. use lambda specify grandchildren of multiple children rather single child:

var exams = _examsrepository            .getall()            .where(q => q.subjectid == subjectid)            .include(q => q.tests.select(t => t.usertests))            .tolist(); 

note there's no need 2 include calls because children implicitly included if you're including grandchildren.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -