Entity Framework Nested Table Relation SelectMany -


my problem following query return 0 records.but there records in database.

my aim subjectquestion select table subjectid equal questionmodel.

how can do. thank you.

[my models]

subjectquestion

public  class subjectquestion {     public int id { get; set; }     public int subjectid { get; set; }     public int questionid { get; set; }     public virtual list<question.question> question { get; set; } } 

question

public class question {     public int id { get; set; }     public int questionno { get; set; }     public string title { get; set; }     public string description { get; set; }     public bool isdeleted { get; set; }     public questionmodel tomodel()    {        return new questionmodel(id,questionno,title,description,choiceid);    }     } 

questionmodel

public class questionmodel {     public questionmodel(int id, int questionno, string title, string description,int choiceid)     {         id = id;         questionno = questionno;         title = title;         description = description;         choiceid = choiceid;     }     public int id { get; set; }     public int questionno { get; set; }      public string title { get; set; }     public string description { get; set; }     public bool isdeleted { get; set; }  }  

entity framework query

list<questionmodel> lst = _db.subjectquestions.where(x => x.subjectid == subjectid).tolist().selectmany(r=> r.question.select(y=> y.tomodel()).tolist()).tolist(); 

the main problem here

_db.subjectquestions.where(x =>   x.subjectid == subjectid).tolist() 

at point executing query against subjectquestions, ef brings across table , not list of questions. if use include statement questions, should fix it.

_db.subjectquestions.include(x => x.questions).where(x =>   x.subjectid == subjectid).tolist() 

addtionally, if me, make relationship subjectquestions explicit on questions class (by including subjectquestion property , id). query be

_db.questions.where(x =>   x.subjectquestion.subjectid == subjectid).tolist().select(y=> y.tomodel()) 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -