0

我有一个包含以下列的数据表

CallTime   CallType

15:45:00    Voice
15:30:54    Voice
00:12:14    SMS
01:13:47    Voice
05:48:23    SMS
12:00:47    Voice

现在我想要像这样使用 Linq 结果

Hrs   Count
00    1
01    1
05    1
12    1
15    2

我尝试过使用 Linq 进行 Group,但没有得到正确的结果。

我使用了以下代码

   var groupQuery = from table in Foundrows.AsEnumerable()
                         group table by table["Call Time"] into groupedTable
                         select new
                         {
                             CallTime = groupedTable.Key,
                             CallCount = groupedTable.Count()
                         };
4

2 回答 2

3
var groupQuery = from r in Foundrows.AsEnumerable()
                 group r by r.Field<TimeSpan>("CallTime").Hours into g
                 select new {
                    Hrs = g.Key,
                    CallCount = g.Count()
                 };

如果您的类型列,string则只需将其值解析为TimeSpan

var groupQuery = from r in Foundrows.AsEnumerable()
                 let time = TimeSpan.Parse(r.Field<string>("CallTime"))
                 group r by time.Hours into g
                 select new {
                    Hrs = g.Key,
                    CallCount = g.Count()
                 };

Foundrows.AsEnumerable()
         .Select(r => {
             TimeSpan time;
             return new {
                Row = r,
                Time = TimeSpan.TryParse(r.Field<string>("CallTime"), out time) ?
                       time : (TimeSpan?)null
             };
         })
         .Where(x => x.Time.HasValue)
         .GroupBy(x => x.Time.Value.Hours)
         .Select(g => new {
             Hrs = g.Key,
             CallCount = g.Count()
         });

或者您可以使用TryParsewhich 将返回可空TimeSpan方法,如下所示:

public static TimeSpan? TryParse(string s)
{
    TimeSpan time;
    if (TimeSpan.TryParse(s, out time))
       return time;

    return null;
}

并在原始查询中使用它:

var groupQuery = from r in Foundrows.AsEnumerable()
                 let time = TryParse(r.Field<string>("CallTime"))
                 where time.HasValue
                 group r by time.Value.Hours into g
                 select new {
                    Hrs = g.Key,
                    CallCount = g.Count()
                 };
于 2013-04-16T06:59:55.140 回答
2

如果您想按小时分组并且“呼叫时间”字段是一个DateTime字段:

var groupQuery = from row in Foundrows.AsEnumerable()
                 let hour = row.Field<DateTime>("Call Time").Hour 
                 group row by hour into hourGroup
                 select new {
                    CallHour = hourGroup.Key,
                    CallCount = hourGroup.Count()
                 }; 

如果要按Date+的组合进行分组Hour

var groupQuery = from row in Foundrows.AsEnumerable()
                 let date = row.Field<DateTime>("Call Time")
                 let hour = date.Hour
                 group row by new{ date, hour } into dateHourGroup
                 select new {
                     CallDate = dateHourGroup.Key.date,
                     CallHour = dateHourGroup.Key.hour,
                     CallCount = dateHourGroup.Count()
                 }; 

编辑:如果它实际上是一个TimeSpan(看起来是这样),请使用lazyberezovsky的方法:)

您的评论:如果我想使用TryParse而不是Parse,我需要在代码中进行哪些更改?

我建议创建一个允许返回 a 的扩展Nullable<TimeSpan>,然后您可以检查是否HasValue==true

public static TimeSpan? TryGetTimeSpan(this string tsString, string format = null)
{
    TimeSpan ts;
    bool success;
    if (format == null)
        success = TimeSpan.TryParse(tsString, out ts);
    else
        success = TimeSpan.TryParseExact(tsString, format, null, TimeSpanStyles.None, out ts);
    return success ? (TimeSpan?)ts : (TimeSpan?)null;
} 

现在此查询有效地工作并且不使用未记录的副作用(作为TimeSpan您在查询中使用的局部变量TimeSpan.TryParse):

var groupQuery = from r in Foundrows.AsEnumerable()
                 let time = r.Field<string>("CallTime").TryGetTimeSpan()
                 where time.HasValue
                 group r by time.Value.Hours into g
                 select new
                 {
                     Hrs = g.Key,
                     CallCount = g.Count()
                 };
于 2013-04-16T07:04:04.797 回答