实体框架条件选择
Id1 | Id2 | Date | State |
---|---|---|---|
1 | 1 | 2022-03-14 | Failed |
1 | 1 | 2022-03-15 | Success |
鉴于上面的数据库表,我正在尝试编写 EF 查询,其中我们显示每个 Id Id2 组合的最新记录,但失败将永远保留在列表中。只有上周的成功才会被包括在内。
我认为我正在努力解决的部分是,如果给定的 Id Id2 组合存在失败,但未来会成功,则不应返回失败。
关于从哪里开始有什么建议吗?上一次尝试尝试执行某项操作
.Where(x => x.state == 'Failed' || x.CreatedOn > DateTime.Now.AddDays(-7)
,但 10 天前失败,随后 9 天前成功,已列出。 (这种情况应该不返回任何内容,因为成功已经在一周前了)
基本上所有失败,除了后来成功的 UNION 与过去 2 天的成功。 这可能吗?我从来没有在两个表之间的链接上花费这么长时间。
Id1 | Id2 | Date | State |
---|---|---|---|
1 | 1 | 2022-03-14 | Failed |
1 | 1 | 2022-03-15 | Success |
Given the above database table, I am trying to write EF query where we show the latest record for each Id Id2 combo but failures will stay listed forever. Successes will only be included if it was in the last week.
The part I think I am struggling with is that If a failure exists for a given Id Id2 combo, but has a future success that failure shouldn't be returned.
Any suggestions on where to begin? The previous attempt tried to do a
.Where(x => x.state == 'Failed' || x.CreatedOn > DateTime.Now.AddDays(-7)
but a fail 10 days ago followed by a success 9 days ago was being listed. (This case should return nothing since the success was over a week ago)
Basically all fails except where a later success UNION with success last 2 days.
Is this even possible? I've never spent this long on a linq between 2 tables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先一个问题:如果最近的状态是失败,是否只想报告自上次成功以来的失败?换句话说,对于序列{失败,失败,成功,失败,失败},您只想报告最后两个失败吗?我假设,是的。
在我看来,您首先需要按 Id2 进行分组,并为每个组计算两件事:
由此,您可以选择该组的成员,其中:
类似
免责声明的内容 - 以上未选中,并且语法上可能不是100%正确,但传达了我的想法。我确信还有优化的空间,例如预先订购每个组的成员。
我希望上面的代码能够完成内存中的大部分工作。我不确定是否可以将其编码为可转换为服务器端 SQL 查询的形式。比我更有技能的人可能会提供解决方案。目标是生成如下内容:
First a question: If the latest state is a failure, do you only want to report the failures since the last success? In other words for the sequence {Failure, Failure, Success, Failure, Failure} do you only want to report the last two Failures? I will assume, yes.
It seems to me that you first need to group by Id2 and calculate two things for each group:
From that, you can select members of the group where either:
Something like
Disclaimer - The above is unchecked and might not be 100% syntactically correct, but conveys my ideas. There are also I'm sure room for optimizations, such as pre-ordering the members of each group.
I expect that the above will perform most of the work in memory. I am not sure if this can be coded in a form that will translate to a server side SQL query. Someone with more skills than I may offer a solution. The goal would be to generate something like:
我不确定这是否可以转换为 EF 查询,但我想我会发布它以防万一(或者万一它可以提供一些灵感)。这是一种 Linq to Objects 方法,应该会产生所需的输出。
如果您的类定义如下:
并且您的项目存储在
Item
集合中,您可以:Id1
和Id2
对 在每个组中,选择具有最新Date
值的一项(使用.MaxBy()
).MaxBy( * )
也可以替换为.OrderByDescending( * ).First()
)Id1
-and-Id2
项目集合仅保留State = Failed
或不少于一周的项目对于项目集合
List- ; items
,它可以按如下方式实现:示例小提琴此处。
I am not sure this is translatable to an EF query, but thought I'd post it in case it is (or in case it could provide some inspiration). This is a Linq to Objects approach that should produce the desired output.
If your class is defined as follows:
and your items are stored in an
Item
collection, you can:Id1
andId2
Date
value (using.MaxBy()
).MaxBy( * )
could alternatively be replaced with.OrderByDescending( * ).First()
)Id1
-and-Id2
item collection to keep only the items that either haveState = Failed
or are no less than a week oldFor an item collection
List<Item> items
, it could be implemented as follows:Example fiddle here.