【发布时间】:2021-06-29 18:23:31
【问题描述】:
我正在使用 EF6 将以下 SQL 语句转换为 LINQ 寻求帮助。我花了似乎永远在寻找不同的例子,但没有找到任何有效的方法。我看过 linqer(那里没有成功)我看过 linqpad(它不会将 sql 转换为 linq)
下面的查询以 SQL 格式返回我正在寻找的内容,目标是使用 Group 的 WHERE 子句基于内部分组(使用分组作为 DISTINCT 查询)返回表的所有列将记录集过滤为所需的内容,并加入 [CdrCallId] 上的内部分组仅返回 [CdrCallId] 匹配的记录。
SELECT ct1.StartTime, ct1.CdrCallID, ct1.CallingNumberId, ct1.CalledNumberId, ct1.ThreadSequence
FROM CallTransactions as ct1
join (select CdrCallID
from CallTransactions as ct2
WHERE [StartTime] >= '10/1/2020 00:00:00 AM' AND [StartTime] <= '03/31/2021 00:00:00 AM' AND [CalledNumberId] = '1670589' OR [CallingNumberId] = '1670589' OR [DestinationNumberId] = '1670589' OR [TransferringNumberId] = '1670589' OR [KeyPartyNumberId] = '1670589'
group by ct2.CdrCallID) ct2
on ct1.CdrCallID = ct2.CdrCallID
| StartTime | CdrCallID | CallingNumberId | CalledNumberId | ThreadSequence |
|---|---|---|---|---|
| 2020-11-02 12:49:34.007 | 995368-307-63751883929019 | 1670589 | 1658625 | 995368 |
| 2021-02-19 14:38:54.600 | 78900-050-63751893781085 | 1670589 | 1658625 | 78900 |
| 2020-10-27 09:58:15.007 | 704239-301-63751883392147 | 1663834 | 1667952 | 704239 |
| 2020-10-27 09:58:15.007 | 704239-301-63751883392147 | 1663834 | 1670589 | 704239 |
| 2020-10-27 09:57:14.007 | 704239-301-63751883392147 | 1663834 | 1667952 | 704239 |
| 2020-10-27 09:57:59.000 | 704239-301-63751883392147 | 1663834 | 1670589 | 704239 |
| 2020-11-02 10:15:06.007 | 497923-307-63751883688115 | 1663847 | 1670589 | 497923 |
我一直在努力寻找合适的 LINQ 方法语法来模仿上述查询。
【问题讨论】:
-
我不相信 SQL 会返回您需要的内容(可能在您尝试的较小子集上)。您确定所有这些 AND、OR 吗?您能否提供一些数据和所需的输出
标签: c# sql-server linq entity-framework-6