Include in query lambda c#












0














I have three models related Subject, UserSchools and Enroll_Teacher_Subjects. I want to make a query that get the information of UserSchools and the relations that Enroll_Teacher_Subjects have had with UserSchools. This works for me but I also need the information the object Subject that is inside the Enroll_Teacher_Subjects because I need NameSubject, DescriptionSubject. Always is null and I Cann´t place it in the include the query lambda.



the model Subject



public class Subject
{
[Key]
public int SubjectId { get; set; }

[Required]
[StringLength(50)]//only you can to write 50 characters
[Display(Name = "Nombre de la Materia")]
public string NameSubject { get; set; }


[DataType(DataType.MultilineText)]
[Display(Name = "Descripcion de la Materia")]
public string DescriptionSubject { get; set; }

[Display(Name = "Grado")]
public int LevelId { get; set; }

public virtual Level Level { get; set; }

public virtual ICollection<Enroll_Teacher_Subject> Enroll_Teacher_Subjects { get; set; }
}


the model UserSchool



public class UserSchool
{

[Key]
public int UserId { get; set; }

[Display(Name = "E-Mail")]
[Required(ErrorMessage = "The field {0} is required")]
[StringLength(50)]//only you can to write 50 characters
[DataType(DataType.EmailAddress)]
// [Index("UserNameIndex", Isnique = true)]
public string EmailUser { get; set; }

[Display(Name = "Nombres")]
[Required]
public string FirstNameUser { get; set; }

[Display(Name = "Apellidos")]
[Required]
public string LastNameUser { get; set; }

[Display(Name = "Full Name")]
public string FullName
{
get { return string.Format("{0} {1}", this.FirstNameUser, this.LastNameUser); }
}
[Display(Name = "Telefono")]
public string PhoneUser { get; set; }

[Display(Name = "Direccion de residencia")]
[Required]
public string AddressUser { get; set; }

[Display(Name = "Fecha de nacimiento")]
[DataType(DataType.Date)]// para que nos muestre el calendario
public DateTime DateBirthdayUser{ get; set; }


[Display(Name = "Imagen")]
public string PhotoUser { get; set; }

[Display(Name = "Estudiante")]
public bool IsStudentUser { get; set; }

[Display(Name = "Profesor")]
public bool IsTeacherUser { get; set; }

[Display(Name = "Sistemas")]
public bool IsSystemUser { get; set; }

[Display(Name = "Numero de documento")]
public string NumberDocumentUsers { get; set; }

[Display(Name = "Tipo de documento")]
public int TypeDocumentId { get; set; }
public virtual TypeDocument TypeDocument { get; set; }

[Display(Name = "Pais de nacimiento")]
public int CountryId { get; set; }
public virtual Country Country { get; set; }

[Display(Name = "Departamento de nacimiento")]
public int StateId { get; set; }
public virtual State State { get; set; }

[Display(Name = "Ciudad de nacimento")]
public int CityId { get; set; }
public virtual City City { get; set; }

public virtual ICollection<Enroll_Teacher_Subject> Enroll_Teacher_Subjects { get; set; }
}


The model Enroll_Teacher_Subject



 public class Enroll_Teacher_Subject
{
[Key]
public int Enroll_Teacher_SubjectId { get; set; }

[Required]
[Display(Name = "Fecha Matricula ")]
[DataType(DataType.Date)]
public DateTime DateEnroll_Teacher_Subject { get; set; }


[DataType(DataType.MultilineText)]
[Display(Name = "Observaciones")]
public string ObservationEnroll_Teacher_Subject { get; set; }



[Display(Name = "Profesor")]
public int UserId { get; set; }

public virtual UserSchool UserSchool { get; set; }

[Display(Name = "Materia")]
public int SubjectId { get; set; }

public virtual Subject Subject { get; set; }

[Display(Name = "Anno lectivo")]
public int AnnoLectivoId { get; set; }
public virtual AnnoLectivo AnnoLectivo { get; set; }

public virtual ICollection<Enroll_Student_Subject_Teacher> Enroll_Student_Subject_Teachers { get; set; }

}


the query



public async Task<ActionResult> Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}

var UserList = await db.UserSchools
.Include(x => x.Enroll_Teacher_Subjects)

.Include(x => x.TypeDocument)
.FirstOrDefaultAsync(x => x.UserId == id);

if (UserList == null)
{
return HttpNotFound();
}

return View(UserList);
}


my query



enter image description here



The object is null and I want that it give information about the subject



enter image description here










share|improve this question
























  • Model of Enroll_Teacher_Subject seems to be missing...
    – ZorgoZ
    Nov 20 '18 at 19:29










  • I'm sorry, add the model
    – Jhon Alexander Jimenez Morales
    Nov 20 '18 at 20:02










  • See stackoverflow.com/questions/48430824/…. You need to explicitly include the Subject when navigating from Teacher. Depending on the EF version (6 or Core), you'd need Select inside Include or ThenInclude.
    – Ivan Stoev
    Nov 20 '18 at 20:35










  • Thanks, Select inside Include it worked
    – Jhon Alexander Jimenez Morales
    Nov 20 '18 at 20:53
















0














I have three models related Subject, UserSchools and Enroll_Teacher_Subjects. I want to make a query that get the information of UserSchools and the relations that Enroll_Teacher_Subjects have had with UserSchools. This works for me but I also need the information the object Subject that is inside the Enroll_Teacher_Subjects because I need NameSubject, DescriptionSubject. Always is null and I Cann´t place it in the include the query lambda.



the model Subject



public class Subject
{
[Key]
public int SubjectId { get; set; }

[Required]
[StringLength(50)]//only you can to write 50 characters
[Display(Name = "Nombre de la Materia")]
public string NameSubject { get; set; }


[DataType(DataType.MultilineText)]
[Display(Name = "Descripcion de la Materia")]
public string DescriptionSubject { get; set; }

[Display(Name = "Grado")]
public int LevelId { get; set; }

public virtual Level Level { get; set; }

public virtual ICollection<Enroll_Teacher_Subject> Enroll_Teacher_Subjects { get; set; }
}


the model UserSchool



public class UserSchool
{

[Key]
public int UserId { get; set; }

[Display(Name = "E-Mail")]
[Required(ErrorMessage = "The field {0} is required")]
[StringLength(50)]//only you can to write 50 characters
[DataType(DataType.EmailAddress)]
// [Index("UserNameIndex", Isnique = true)]
public string EmailUser { get; set; }

[Display(Name = "Nombres")]
[Required]
public string FirstNameUser { get; set; }

[Display(Name = "Apellidos")]
[Required]
public string LastNameUser { get; set; }

[Display(Name = "Full Name")]
public string FullName
{
get { return string.Format("{0} {1}", this.FirstNameUser, this.LastNameUser); }
}
[Display(Name = "Telefono")]
public string PhoneUser { get; set; }

[Display(Name = "Direccion de residencia")]
[Required]
public string AddressUser { get; set; }

[Display(Name = "Fecha de nacimiento")]
[DataType(DataType.Date)]// para que nos muestre el calendario
public DateTime DateBirthdayUser{ get; set; }


[Display(Name = "Imagen")]
public string PhotoUser { get; set; }

[Display(Name = "Estudiante")]
public bool IsStudentUser { get; set; }

[Display(Name = "Profesor")]
public bool IsTeacherUser { get; set; }

[Display(Name = "Sistemas")]
public bool IsSystemUser { get; set; }

[Display(Name = "Numero de documento")]
public string NumberDocumentUsers { get; set; }

[Display(Name = "Tipo de documento")]
public int TypeDocumentId { get; set; }
public virtual TypeDocument TypeDocument { get; set; }

[Display(Name = "Pais de nacimiento")]
public int CountryId { get; set; }
public virtual Country Country { get; set; }

[Display(Name = "Departamento de nacimiento")]
public int StateId { get; set; }
public virtual State State { get; set; }

[Display(Name = "Ciudad de nacimento")]
public int CityId { get; set; }
public virtual City City { get; set; }

public virtual ICollection<Enroll_Teacher_Subject> Enroll_Teacher_Subjects { get; set; }
}


The model Enroll_Teacher_Subject



 public class Enroll_Teacher_Subject
{
[Key]
public int Enroll_Teacher_SubjectId { get; set; }

[Required]
[Display(Name = "Fecha Matricula ")]
[DataType(DataType.Date)]
public DateTime DateEnroll_Teacher_Subject { get; set; }


[DataType(DataType.MultilineText)]
[Display(Name = "Observaciones")]
public string ObservationEnroll_Teacher_Subject { get; set; }



[Display(Name = "Profesor")]
public int UserId { get; set; }

public virtual UserSchool UserSchool { get; set; }

[Display(Name = "Materia")]
public int SubjectId { get; set; }

public virtual Subject Subject { get; set; }

[Display(Name = "Anno lectivo")]
public int AnnoLectivoId { get; set; }
public virtual AnnoLectivo AnnoLectivo { get; set; }

public virtual ICollection<Enroll_Student_Subject_Teacher> Enroll_Student_Subject_Teachers { get; set; }

}


the query



public async Task<ActionResult> Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}

var UserList = await db.UserSchools
.Include(x => x.Enroll_Teacher_Subjects)

.Include(x => x.TypeDocument)
.FirstOrDefaultAsync(x => x.UserId == id);

if (UserList == null)
{
return HttpNotFound();
}

return View(UserList);
}


my query



enter image description here



The object is null and I want that it give information about the subject



enter image description here










share|improve this question
























  • Model of Enroll_Teacher_Subject seems to be missing...
    – ZorgoZ
    Nov 20 '18 at 19:29










  • I'm sorry, add the model
    – Jhon Alexander Jimenez Morales
    Nov 20 '18 at 20:02










  • See stackoverflow.com/questions/48430824/…. You need to explicitly include the Subject when navigating from Teacher. Depending on the EF version (6 or Core), you'd need Select inside Include or ThenInclude.
    – Ivan Stoev
    Nov 20 '18 at 20:35










  • Thanks, Select inside Include it worked
    – Jhon Alexander Jimenez Morales
    Nov 20 '18 at 20:53














0












0








0







I have three models related Subject, UserSchools and Enroll_Teacher_Subjects. I want to make a query that get the information of UserSchools and the relations that Enroll_Teacher_Subjects have had with UserSchools. This works for me but I also need the information the object Subject that is inside the Enroll_Teacher_Subjects because I need NameSubject, DescriptionSubject. Always is null and I Cann´t place it in the include the query lambda.



the model Subject



public class Subject
{
[Key]
public int SubjectId { get; set; }

[Required]
[StringLength(50)]//only you can to write 50 characters
[Display(Name = "Nombre de la Materia")]
public string NameSubject { get; set; }


[DataType(DataType.MultilineText)]
[Display(Name = "Descripcion de la Materia")]
public string DescriptionSubject { get; set; }

[Display(Name = "Grado")]
public int LevelId { get; set; }

public virtual Level Level { get; set; }

public virtual ICollection<Enroll_Teacher_Subject> Enroll_Teacher_Subjects { get; set; }
}


the model UserSchool



public class UserSchool
{

[Key]
public int UserId { get; set; }

[Display(Name = "E-Mail")]
[Required(ErrorMessage = "The field {0} is required")]
[StringLength(50)]//only you can to write 50 characters
[DataType(DataType.EmailAddress)]
// [Index("UserNameIndex", Isnique = true)]
public string EmailUser { get; set; }

[Display(Name = "Nombres")]
[Required]
public string FirstNameUser { get; set; }

[Display(Name = "Apellidos")]
[Required]
public string LastNameUser { get; set; }

[Display(Name = "Full Name")]
public string FullName
{
get { return string.Format("{0} {1}", this.FirstNameUser, this.LastNameUser); }
}
[Display(Name = "Telefono")]
public string PhoneUser { get; set; }

[Display(Name = "Direccion de residencia")]
[Required]
public string AddressUser { get; set; }

[Display(Name = "Fecha de nacimiento")]
[DataType(DataType.Date)]// para que nos muestre el calendario
public DateTime DateBirthdayUser{ get; set; }


[Display(Name = "Imagen")]
public string PhotoUser { get; set; }

[Display(Name = "Estudiante")]
public bool IsStudentUser { get; set; }

[Display(Name = "Profesor")]
public bool IsTeacherUser { get; set; }

[Display(Name = "Sistemas")]
public bool IsSystemUser { get; set; }

[Display(Name = "Numero de documento")]
public string NumberDocumentUsers { get; set; }

[Display(Name = "Tipo de documento")]
public int TypeDocumentId { get; set; }
public virtual TypeDocument TypeDocument { get; set; }

[Display(Name = "Pais de nacimiento")]
public int CountryId { get; set; }
public virtual Country Country { get; set; }

[Display(Name = "Departamento de nacimiento")]
public int StateId { get; set; }
public virtual State State { get; set; }

[Display(Name = "Ciudad de nacimento")]
public int CityId { get; set; }
public virtual City City { get; set; }

public virtual ICollection<Enroll_Teacher_Subject> Enroll_Teacher_Subjects { get; set; }
}


The model Enroll_Teacher_Subject



 public class Enroll_Teacher_Subject
{
[Key]
public int Enroll_Teacher_SubjectId { get; set; }

[Required]
[Display(Name = "Fecha Matricula ")]
[DataType(DataType.Date)]
public DateTime DateEnroll_Teacher_Subject { get; set; }


[DataType(DataType.MultilineText)]
[Display(Name = "Observaciones")]
public string ObservationEnroll_Teacher_Subject { get; set; }



[Display(Name = "Profesor")]
public int UserId { get; set; }

public virtual UserSchool UserSchool { get; set; }

[Display(Name = "Materia")]
public int SubjectId { get; set; }

public virtual Subject Subject { get; set; }

[Display(Name = "Anno lectivo")]
public int AnnoLectivoId { get; set; }
public virtual AnnoLectivo AnnoLectivo { get; set; }

public virtual ICollection<Enroll_Student_Subject_Teacher> Enroll_Student_Subject_Teachers { get; set; }

}


the query



public async Task<ActionResult> Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}

var UserList = await db.UserSchools
.Include(x => x.Enroll_Teacher_Subjects)

.Include(x => x.TypeDocument)
.FirstOrDefaultAsync(x => x.UserId == id);

if (UserList == null)
{
return HttpNotFound();
}

return View(UserList);
}


my query



enter image description here



The object is null and I want that it give information about the subject



enter image description here










share|improve this question















I have three models related Subject, UserSchools and Enroll_Teacher_Subjects. I want to make a query that get the information of UserSchools and the relations that Enroll_Teacher_Subjects have had with UserSchools. This works for me but I also need the information the object Subject that is inside the Enroll_Teacher_Subjects because I need NameSubject, DescriptionSubject. Always is null and I Cann´t place it in the include the query lambda.



the model Subject



public class Subject
{
[Key]
public int SubjectId { get; set; }

[Required]
[StringLength(50)]//only you can to write 50 characters
[Display(Name = "Nombre de la Materia")]
public string NameSubject { get; set; }


[DataType(DataType.MultilineText)]
[Display(Name = "Descripcion de la Materia")]
public string DescriptionSubject { get; set; }

[Display(Name = "Grado")]
public int LevelId { get; set; }

public virtual Level Level { get; set; }

public virtual ICollection<Enroll_Teacher_Subject> Enroll_Teacher_Subjects { get; set; }
}


the model UserSchool



public class UserSchool
{

[Key]
public int UserId { get; set; }

[Display(Name = "E-Mail")]
[Required(ErrorMessage = "The field {0} is required")]
[StringLength(50)]//only you can to write 50 characters
[DataType(DataType.EmailAddress)]
// [Index("UserNameIndex", Isnique = true)]
public string EmailUser { get; set; }

[Display(Name = "Nombres")]
[Required]
public string FirstNameUser { get; set; }

[Display(Name = "Apellidos")]
[Required]
public string LastNameUser { get; set; }

[Display(Name = "Full Name")]
public string FullName
{
get { return string.Format("{0} {1}", this.FirstNameUser, this.LastNameUser); }
}
[Display(Name = "Telefono")]
public string PhoneUser { get; set; }

[Display(Name = "Direccion de residencia")]
[Required]
public string AddressUser { get; set; }

[Display(Name = "Fecha de nacimiento")]
[DataType(DataType.Date)]// para que nos muestre el calendario
public DateTime DateBirthdayUser{ get; set; }


[Display(Name = "Imagen")]
public string PhotoUser { get; set; }

[Display(Name = "Estudiante")]
public bool IsStudentUser { get; set; }

[Display(Name = "Profesor")]
public bool IsTeacherUser { get; set; }

[Display(Name = "Sistemas")]
public bool IsSystemUser { get; set; }

[Display(Name = "Numero de documento")]
public string NumberDocumentUsers { get; set; }

[Display(Name = "Tipo de documento")]
public int TypeDocumentId { get; set; }
public virtual TypeDocument TypeDocument { get; set; }

[Display(Name = "Pais de nacimiento")]
public int CountryId { get; set; }
public virtual Country Country { get; set; }

[Display(Name = "Departamento de nacimiento")]
public int StateId { get; set; }
public virtual State State { get; set; }

[Display(Name = "Ciudad de nacimento")]
public int CityId { get; set; }
public virtual City City { get; set; }

public virtual ICollection<Enroll_Teacher_Subject> Enroll_Teacher_Subjects { get; set; }
}


The model Enroll_Teacher_Subject



 public class Enroll_Teacher_Subject
{
[Key]
public int Enroll_Teacher_SubjectId { get; set; }

[Required]
[Display(Name = "Fecha Matricula ")]
[DataType(DataType.Date)]
public DateTime DateEnroll_Teacher_Subject { get; set; }


[DataType(DataType.MultilineText)]
[Display(Name = "Observaciones")]
public string ObservationEnroll_Teacher_Subject { get; set; }



[Display(Name = "Profesor")]
public int UserId { get; set; }

public virtual UserSchool UserSchool { get; set; }

[Display(Name = "Materia")]
public int SubjectId { get; set; }

public virtual Subject Subject { get; set; }

[Display(Name = "Anno lectivo")]
public int AnnoLectivoId { get; set; }
public virtual AnnoLectivo AnnoLectivo { get; set; }

public virtual ICollection<Enroll_Student_Subject_Teacher> Enroll_Student_Subject_Teachers { get; set; }

}


the query



public async Task<ActionResult> Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}

var UserList = await db.UserSchools
.Include(x => x.Enroll_Teacher_Subjects)

.Include(x => x.TypeDocument)
.FirstOrDefaultAsync(x => x.UserId == id);

if (UserList == null)
{
return HttpNotFound();
}

return View(UserList);
}


my query



enter image description here



The object is null and I want that it give information about the subject



enter image description here







.net asp.net-mvc linq lambda






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 20:01

























asked Nov 20 '18 at 19:26









Jhon Alexander Jimenez Morales

12




12












  • Model of Enroll_Teacher_Subject seems to be missing...
    – ZorgoZ
    Nov 20 '18 at 19:29










  • I'm sorry, add the model
    – Jhon Alexander Jimenez Morales
    Nov 20 '18 at 20:02










  • See stackoverflow.com/questions/48430824/…. You need to explicitly include the Subject when navigating from Teacher. Depending on the EF version (6 or Core), you'd need Select inside Include or ThenInclude.
    – Ivan Stoev
    Nov 20 '18 at 20:35










  • Thanks, Select inside Include it worked
    – Jhon Alexander Jimenez Morales
    Nov 20 '18 at 20:53


















  • Model of Enroll_Teacher_Subject seems to be missing...
    – ZorgoZ
    Nov 20 '18 at 19:29










  • I'm sorry, add the model
    – Jhon Alexander Jimenez Morales
    Nov 20 '18 at 20:02










  • See stackoverflow.com/questions/48430824/…. You need to explicitly include the Subject when navigating from Teacher. Depending on the EF version (6 or Core), you'd need Select inside Include or ThenInclude.
    – Ivan Stoev
    Nov 20 '18 at 20:35










  • Thanks, Select inside Include it worked
    – Jhon Alexander Jimenez Morales
    Nov 20 '18 at 20:53
















Model of Enroll_Teacher_Subject seems to be missing...
– ZorgoZ
Nov 20 '18 at 19:29




Model of Enroll_Teacher_Subject seems to be missing...
– ZorgoZ
Nov 20 '18 at 19:29












I'm sorry, add the model
– Jhon Alexander Jimenez Morales
Nov 20 '18 at 20:02




I'm sorry, add the model
– Jhon Alexander Jimenez Morales
Nov 20 '18 at 20:02












See stackoverflow.com/questions/48430824/…. You need to explicitly include the Subject when navigating from Teacher. Depending on the EF version (6 or Core), you'd need Select inside Include or ThenInclude.
– Ivan Stoev
Nov 20 '18 at 20:35




See stackoverflow.com/questions/48430824/…. You need to explicitly include the Subject when navigating from Teacher. Depending on the EF version (6 or Core), you'd need Select inside Include or ThenInclude.
– Ivan Stoev
Nov 20 '18 at 20:35












Thanks, Select inside Include it worked
– Jhon Alexander Jimenez Morales
Nov 20 '18 at 20:53




Thanks, Select inside Include it worked
– Jhon Alexander Jimenez Morales
Nov 20 '18 at 20:53

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400176%2finclude-in-query-lambda-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400176%2finclude-in-query-lambda-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin