你好:我有两张 table 。
表格:
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 |
-------------------------
我现在的代码:
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>();
}
主要思想是,如果我获得类型和 ID 值,我应该获得作业表中未提及的所有用户 JobID == id 并且如果他们有城市 == 类型。现在,它会返回提到的和未提到的,如果我删除 Distinct(),它会返回许多重复项。有谁知道如何解决这个问题?谢谢!
已解决:
谢谢大家!!!我找到了答案,这段代码实际上已经开始正常工作了: 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>();
}
请您参考如下方法:
我更改了返回类型,如果我没理解错的话,你想接受那些没有工作的人。
public IEnumerable<Person> GetAllMaterialsByTypeNotSelected(string type , int id)
{
return dataContext.Person
.Where(p => dataContext.Job.FirstOrDefault(j => j.PersonId == p.PersonId)== null);
}
