如何编写这个 LINQ 查询

问题描述 投票:0回答:2

嗨,我有一个包含以下字段的数据表

DAT_START
GROUPBY
TXT_LATITTUDE
TXT_LONGITUDE
INT_DIRECTION
INT_CALL_DATA_TYPE
LNG_DURATION

以下是我正在使用的 LINQ 查询

var data = (from r in dt.AsEnumerable()
where ((r.Field<DateTime>("DAT_START").TimeOfDay.Hours < 20) && (r.Field<DateTime>("DAT_START").TimeOfDay.Hours >= 4))
group r by new { CID = r["GroupBy"], CLatitude = r["TXT_LATITUDE"], CLongitude = r["TXT_LONGITUDE"],CDirection = r["INT_DIRECTION"], 
CCallType = r["INT_CALL_DATA_TYPE"],CDuration = r["LNG_DURATION"] }
into groupedTable
select new
{
   CellID = groupedTable.Key.CID,
   CallCount = groupedTable.Count(),
   Longitude = groupedTable.Key.CLongitude,
   Latitude = groupedTable.Key.CLatitude,
   Direction = groupedTable.Key.CDirection,
   CallType = groupedTable.Key.CCallType,
   Duration = groupedTable.Key.CDuration
}).OrderByDescending(s => s.CallCount);

它给了我这样的结果

CellID = 4057,CallCount = 84,Longitude = "",Latitude = "",Direction = "Incoming",CallType = "Voice",Duration = 50
CellID = 4057,CallCount = 8,Longitude = "",Latitude = "",Direction = "Outgoing",CallType = "Voice",Duration =97
CellID = 4057,CallCount = 56,Longitude = "",Latitude = "",Direction = "Incoming",CallType ="SMS" ,Duration = 0
CellID = 4057,CallCount = 41,Longitude = "",Latitude = "",Direction = "Outgoing",CallType = "SMS",Duration = 0

现在我想要这样的结果

CellID = 4057, TotalCommCount = 204, TotalDuration = 147, INSMSCount = 56,OutSMSCount = 41, INVoiceCount = 84,OutVoiceCount = 8,InVoiceDuration =50,OutVoiceDuration = 47

我怎样才能做到这一点。看到这里我就震惊了..

c# winforms linq linqpad
2个回答
6
投票

看来您目前分组太多,这就是您获得多行的原因。我怀疑你想要这样的东西:

from r in dt.AsEnumerable()
where r.Field<DateTime>("DAT_START").TimeOfDay.Hours < 20 && 
      r.Field<DateTime>("DAT_START").TimeOfDay.Hours >= 4
group r r["GroupBy"] into g         
select new
{
    CellID = g.Key,
    TotalCommCount = g.Count(),
    TotalDuration = g.Sum(r => r.Field<long>("LNG_DURATION")),
    InSMSCount = g.Count(r => r.Field<string>("DIRECTION") == "Incoming" &&
                              r.Field<string>("CALL_TYPE") == "SMS"),
    OutSMSCount = g.Count(r => r.Field<string>("DIRECTION") == "Outgoing" &&
                               r.Field<string>("CALL_TYPE") == "SMS"), 
    InVoiceCount = g.Count(r => r.Field<string>("DIRECTION") == "Incoming" &&
                                r.Field<string>("CALL_TYPE") == "Voice"),
    OutVoiceCount = g.Count(r => r.Field<string>("DIRECTION") == "Outgoing" &&
                                 r.Field<string>("CALL_TYPE") == "Voice"),
    InVoiceDuration = g.Where(r => r.Field<string>("DIRECTION") == "Incoming" &&
                                r.Field<string>("CALL_TYPE") == "Voice")
                       .Sum(r => r.Field<long>("DURATION"))
    OutVoiceDuration = g.Where(r => r.Field<string>("DIRECTION") == "Outgoing" &&
                                    r.Field<string>("CALL_TYPE") == "Voice"),
                       .Sum(r => r.Field<long>("DURATION"))
 } into summary
 order by summary.TotalCommCount descending
 select summary;

0
投票

我建议您在 SQL 数据库中创建一个视图,您可以在其中简单地聚合所需内容,然后在 Linq 查询中使用该视图 - 这将执行得很快并且易于编写。

设计草图:

由于您的需求描述不完全清楚,我做了一些假设。 创建一个SQL视图例如(只是为了展示想法,根据你的需要更改它):

CREATE VIEW [dbo].[vSumDurations]
AS 
select  c.CellId, j1.SumDuration1 as TotalDuration, 
        j2.SumDuration2 as GroupedDuration, j1.CallCount  
from (select distinct t.CID as CellId from [dbo].[YourTable] t) c
left join (select t1.CID as CellId, sum(LNG_Duration) as SumDuration1, 
            count(*) as CallCount from [dbo].[YourTable] t1 
            Group By t1.CID) j1 
on c.CellId=j1.CellId           
left join (select t2.CID as CellId, sum(LNG_Duration) as SumDuration2 
            from [dbo].[YourTable] t2 
            Group by t2.CID, t2.Direction, t2.CallType) j2 
on c.CellId=j2.CellId

(您知道可以通过在实体图页面的上下文菜单中使用“从数据库更新模型”轻松地将此视图添加到您的 *.EDMX,然后在“表/视图”部分中勾选视图 vSumDurations弹出对话框。)

完成此准备工作后,您的 Linq 查询 非常简单,因为一切都已在视图中完成。因此,代码如下:

var dc = this; // in LinqPad you can set your data context
var data = (from d in dc.vSumDurations select d).OrderByDescending(s => s.CallCount);
data.Dump();

注意: 该示例是为 LinqPad 制作的 - 您在实际代码中将需要不同的数据上下文。 Linqpad 不需要

dc
,但如果您出于方便原因声明它会更容易,因为在您的实际代码中您必须提供它。

© www.soinside.com 2019 - 2024. All rights reserved.