LINQ 连接行为异常

发布于 2024-11-27 04:01:50 字数 946 浏览 1 评论 0原文

我正在尝试在两个表之间执行联接并按 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

¢蛋碎的人ぎ生 2024-12-04 04:01:50

我似乎无法添加注释,但在调试模式下,您应该能够在这行代码上放置一个断点。当您这样做时,您应该能够将鼠标悬停在它上面并让它告诉您 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.

始终不够 2024-12-04 04:01:50

根据您的建议,我发布我的评论作为答案:

“它也可能有助于查看您的架构。metaID 的数据类型,
content_statusID 和 date_created 也可能发挥作用——并且
对我(不熟悉你的代码的人)来说很容易
关于这些数据类型的假设。”

At your suggestion, I'm posting my comment as the answer:

"It might also help to see your schema. The data types for metaID,
content_statusID, and date_created might come into play as well -- and
it's easy for me (somebody who's unfamiliar with your code) to make
assumptions about those data types."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文