LINQ 连接行为异常
我正在尝试在两个表之间执行联接并按 3 个条件限制结果。其中2个条件属于主表,第三个条件属于从表。这是我正在尝试的查询:
var articles = (from article in this.Context.contents
join meta in this.Context.content_meta on article.ID equals meta.contentID
where meta.metaID == 1 && article.content_statusID == 1 && article.date_created > created
orderby article.date_created ascending
select article.content_text_key);
它的目的是通过 contentID 连接两个表,然后根据 metaID(文章类型)、statusID 进行过滤,然后获取大于创建日期时间的所有文章。问题是它返回 2 条记录(当前共 4 条记录)。一个的 date_created
小于 created
,另一个是首先生成 created
的记录(因此相等)。
通过删除元的连接和 where 子句,结果不会产生任何记录(预期)。我无法理解的是,当我将这个连接转换为常规 SQL 时,它工作得很好。显然我误解了 join
在这种情况下的功能。什么会导致这种行为?
编辑:
在 LinqPad 中尝试过此操作后,我注意到 LinqPad 提供了预期的结果。我已经在代码中分别尝试了这些查询,直到添加联接后,奇怪的结果才开始填充它似乎发生在记录与限制器发生在同一天的任何日期比较中。
I am attempting to perform a join between two tables and limit results by 3 conditions. 2 of the conditions belong to the primary table, the third condition belongs to the secondary table. Here is the query I'm attempting:
var articles = (from article in this.Context.contents
join meta in this.Context.content_meta on article.ID equals meta.contentID
where meta.metaID == 1 && article.content_statusID == 1 && article.date_created > created
orderby article.date_created ascending
select article.content_text_key);
It is meant to join the two tables by the contentID, then filter based on the metaID (type of article), statusID, and then get all articles that are greater than the datetime created
. The problem is that it returns 2 records (out of 4 currently). One has a date_created
less than created
and the other is the record that produced created
in the first place (thus equal).
By removing the join and the where clause for the meta, the result produces no records (expected). What I can't understand is that when I translate this join into regular SQL it works just fine. Obviously I'm misunderstanding what the functionality of join
is in this context. What would cause this behavior?
Edit:
Having tried this in LinqPad, I've noticed that LinqPad provides the expected results. I have tried these queries separately in code and it isn't until the join is added that odd results begin populating it appears to be happening on any date comparison where the record occurs on the same day as the limiter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我似乎无法添加注释,但在调试模式下,您应该能够在这行代码上放置一个断点。当您这样做时,您应该能够将鼠标悬停在它上面并让它告诉您 LINQ 生成的 sql。请发布该sql。
I can't seem to be able to add a comment but in debug mode you should be able to put a break point on this line of code. When you do you should be able to hover over it and have it tell you the sql that LINQ generates. Please post that sql.
根据您的建议,我发布我的评论作为答案:
At your suggestion, I'm posting my comment as the answer: