我有一个像这样的简单SQL查询:
SELECT table1.[idGK] , table2.FullName , table2.LgotName
from table2
join table1 on table2.C_LGT = table1.[idGK]
where table1.mcod = 41003
我有正确的输出:
idGK | FullName| LgotName
------------------------
1 |One |Ball
2 |Two |Wog
3 |Three |Aks
5 |Four |Mqi
7 |Five |Thel
9 |Six |Imx
但是当我对此进行LINQ查询时:
IEnumerable<FinalDoc> fidn = from post in repository.table1
join thir in repository.table2 on post.idGK equals thir.C_LGT
where post.mcod.Trim().Contains("41003")
orderby post.idGK
select new FinalDoc
{
mcod = post.mcod,
FullName= thir.FullName,
idGK = post.idGK
};
我有这个结果:
FullName | LgotName
------------------------
Five |Thel
Five |Thel
Five |Thel
Five |Thel
Five |Thel
Five |Thel
我尝试更改table1和表2以进行正确的连接,但我有相同的结果.
我需要做什么linq查询才能在SQl中得到相同的结果?
P.S EF,Linq,Asp.net,Web Forms
最佳答案 LINQ等效的SQL查询是:
from thir in repository.table2
join post in repository.table1 on thir.C_LGT equals post.[idGK]
where post.mcod == 41003
因此,假设您的SQL是正确的(即table1.mcod是数字类型而不是字符串),那么它应该工作.
编辑:
您可以尝试这样来查看SQL EF从LINQ生成的内容.它可能有助于诊断问题.
var query = from post in repository.table1
join thir in repository.table2 on post.idGK equals thir.C_LGT
where post.mcod.Trim().Contains("41003")
var sql = ((System.Data.Objects.ObjectQuery)query).ToTraceString();