Hi there: I have two tables.
Tables:
Person table:
--------------------------------
| id | Namer | Surename | City |
--------------------------------
|1 |aaa |aaa | NewY |
|2 |bbb |bbb | Dall |
|3 |ccc |ccc | Dall |
|4 |ddd |ddd | Dall |
--------------------------------
Job table:
-------------------------
| id | PersonID | JobID |
-------------------------
|1 |1 |1 |
|2 |3 |1 |
|3 |2 |2 |
|4 |3 |2 |
-------------------------
The code I have for now:
C#:
public IEnumerable<Material> GetAllMaterialsByTypeNotSelected(string type , int id)
{
return (from m in dataContext.Person
from cfm in dataContext.Job
where m.Id != cfm.PersonID &&
m.City == type &&
cfm.JobID == id
select m).Distinct().AsEnumerable<Material>();
}
The main idea is if I get type and id values I should get all users that are not mentioned in the Job Tables with JobID == id and if they have city == type. For now, it returns both mentioned and not, and if I remove Distinct() it returns many duplicates. Does anyone know how to solve this problem? Thanks!
Resolved:
Thank you guys!!! I found an answer, this piece of code is actually started to working as it should: C#:
public IEnumerable<Material> GetAllMaterialsByTypeNotSelected(string type , int id)
{
return (from m in dataContext.Person
where !(from o in dataContext.Job
where o.JobID == id
select o.PersonID).Contains(m.Id)&&
m.City == type
select m).Distinct().AsEnumerable<Material>();
}
m.Id != cfm.PersonIDwill not give you "all users that are not mentioned in the Job Tables", it will give you every combination of records in the 2 tables except the ones where that are related by the foreign key.