I’ve had to use a lookup table in an EF project and it took me a bit of tinkering to get it working but I’m unsure how to query something.
If I have three tables (this is a test project to illustrate the question):
Person
------
ID - Int - PK
PersonName - varchar[50]
and
Skill
-----
ID - Int - PK
SkillName - varchar[50]
which are linked by a lookup table:
PS_Lookup
---------
ID - Int - PK
PersonID - Int - FK : Person.ID
SkillID - Int - FK : Skill.ID
Now, if I want to return all users who have a skill of ID 1 I’ve worked out I can do:
var result = (from p in context.People
select new
{
PersonID = p.ID,
PersonName = p.PersonName,
FirstSkill = (from s in p.PS_Lookup
where s.ID == 1
select s.SkillName),
}).ToList();
My question is, what do I need to change on the above query just to return the PersonName and PersonID of everyone who has the skill with the ID 1?
i.e. Not returning “FirstSkill”. I don’t need the name as I know what FirstSkill is and I can’t see what I have to do with the where clause.
You need to use where condition in your query: