sql server - Manually type sql to map to c# object just like .Include("") method -
to make simple here example models. models consist of teacher many students.
public class teacher { public int id { get; set; } public string name { get; set; } public list<student> students { get; set; } public teacher() { students = new list<student>(); } } public class student { public int id { get; set; } public int teacherid { get; set; } [foreignkey("teacherid")] public teacher teacher { get; set; } public string name { get; set; } }
using entityframework teachers , students using linq
context.teachers.include("students");
however, if working large model set need include many child properties query can take time.
my sub question if chain linq statement , select new viewmodel teacher properties need , select students student view model properties need , on.. efficient writing sql manually? entityframework add overhead?
now getting real question. how write query manually include child properties , return in way automatically bind viewmodels?
example:
select teacher.id, teacher.name, student.id, student.name teachers inner join students on teacher.id = student.teacherid
to wouldn't using include
@ all, you'd using select
:
var query = context.teachers.select(teacher => new { teacher.id, teacher.name, students = teacher.students.select(student => new { student.id, student.name, } }
Comments
Post a Comment