如何将以下 SQL 语句转换为 LINQ 方法语法

How to convert the following SQL statement to LINQ Method Syntax

我正在寻求帮助,以使用 EF6 将以下 SQL 语句转换为 LINQ。我似乎一直在研究不同的例子,但没有找到任何有效的方法。我看过 linqer(那里没有成功)我看过 linqpad(它不会将 sql 转换为 linq)

return下面的查询正是我要找的SQL格式,目标是return基于table的所有列内部分组(使用分组作为 DISTINCT 查询)使用 Group 的 WHERE 子句将记录集过滤为所需的内容,并且仅将 [CdrCallId] 上的内部分组加入 return 记录,其中 [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 有一些缺陷,实际上应该是这样的:

SELECT ct1.StartTime, ct1.CdrCallID, ct1.CallingNumberId, ct1.CalledNumberId, ct1.ThreadSequence 
FROM CallTransactions as ct1 
where exists 
(select *
    from CallTransactions as ct2 
     WHERE ct1.CdrCallID = ct2.CdrCallID and 
            (([StartTime] >= '20201001' AND [StartTime] <= '20210331') AND 
           ([CalledNumberId] = '1670589' OR 
           [CallingNumberId] = '1670589' OR 
           [DestinationNumberId] = '1670589' OR 
           [TransferringNumberId] = '1670589' OR 
           [KeyPartyNumberId] = '1670589'));

在 Linq 中类似于:

var start = new DateTime(2020,10,1);
var end = new DateTime(2021,4,1);
var numberId = "1670589";
var callIDs = ctx.CallTransactions
    .Where(x => x.StartTime >= start && x.StartTime < end && 
        (x.CalledNumberId == numberId ||
         x.CallingNumberId == numberId ||
         x.DestinationNumberId == numberId ||
         x.TransferringNumberId == numberId ||
         x.KeyPartNumberId == numberId))
    .Select(x => x.CdrCallId);

var result = ctx.CallTransactions.Where(x => callIDs.Any(ci => ci.CdrCallId == x.CdrCallId)
   .Select(ct1 => new {ct1.StartTime, ct1.CdrCallID, ct1.CallingNumberId, ct1.CalledNumberId, ct1.ThreadSequence});

PS:您真的不需要将所有内容都转换为 EF Linq。您也可以从 Linq 调用原始 SQL。

IQueryable<CallTransactions> items;
var start=new DateTime(2021,1,10);
var due=new DateTime(2021,3,31);

var result=items.Where(a=>a.StartTime>=start && a.StartTime<=due)
                .Where(a=>a.CalledNumberId = '1670589' || 
                          a.CallingNumberId = 1670589 || 
                          a.DestinationNumberId = 1670589 || 
                          a.TransferringNumberId = 1670589 ||
                          a.KeyPartyNumberId = '1670589')
                .Select(a=>new {
                    a.StartTime, 
                    a.CdrCallID, 
                    a.CallingNumberId, 
                    a.CalledNumberId, 
                    a.ThreadSequence
                });

我认为这不需要连接,一旦您过滤了集合,您就拥有了所有需要的行。