Skip to content Skip to sidebar Skip to footer

Ef 6 Select From Other Table Without Navigation Property

I have a small problem which I need a help to solve: I have following situation: For example: I want to select all students who have a dog. I have 2 tables: students id name peti

Solution 1:

Use a join.

var students = (from s in context.students
  join p in context.pets on s.petid equals p.id
  where p.name == "dog"select s).ToList();

For the lambda syntax, you can use this:

var students = context.students.Join(context.pets.Where(p => p.name== "dog"), //filter the petsstudent => student.PetId, //left side key for the joinpet => pet.id, //right side key for the join(student, pet) => student); //what do you want to select

Post a Comment for "Ef 6 Select From Other Table Without Navigation Property"