【发布时间】:2016-12-23 12:47:52
【问题描述】:
我有一个这样的简单 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
我尝试更改表 1 和表 2 以进行正确连接,但结果相同。
我需要做什么 linq 查询才能得到与 SQl 相同的结果?
P.S EF、Linq、Asp.net、Web 表单
【问题讨论】:
-
为什么 SQL 中的
where table1.mcod = 41003和 Linq 中的post.mcod.Trim().Contains("41003")有这种区别? -
可能是因为 sql 查询使用 equal 而 linq 查询使用 contains( sql like "%something%")?这意味着 sql 查询将 41003 与 41003 匹配,而 linq 查询可以将 41003 与 141003、410032、87234741003342 等匹配
-
您是否尝试检查从实体框架生成的正在运行的 SQL 查询?
-
@RajshekarReddy 如果我在 linq 中使用 post.mcod == "41003" 我有空结果
-
@RajshekarReddy 如果我删除 where 子句我拥有所有记录
标签: c# asp.net entity-framework linq