gpt4 book ai didi

c# - 如果值在不同的表中,Linq 查询不会返回正确的结果

转载 作者:太空宇宙 更新时间:2023-11-03 21:28:10 25 4
gpt4 key购买 nike

你好:我有两张 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);
}

关于c# - 如果值在不同的表中,Linq 查询不会返回正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25720698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com