如何在以下情况下查询groupby [英] How to query groupby in following situation
本文介绍了如何在以下情况下查询groupby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下格式的数据表
Id LocationId UserName Startdate
1 10 xz 2017-02-21 09:05:20
2 10 xz 2017-02-21 09:15:20
3 10 xz 2017-02-21 09:25:20
4 10 xz 2017-02- 21 09:35:20
5 11 xz 2017-02-21 09:45:20
6 11 xz 2017-02-21 09:55:20
7 11 xz 2017-02 -21 10:05:20
8 11 xz 2017-02-21 10:15:20
我想计算开始时间结束时间和时间差异,
并有显示记录为
Id locationId startdate starttime endtime timespan
1 10 2017-02-21 09:05 09:35:20 0hr 30min0sec
1 10 2017-02-21 09:05 09:35:20 0小时30分钟sec
我的尝试:
< pre> 如果 dt IsNot Nothing AndAlso dt.Rows.Count> 0 然后
hdnreviewerHistoryCount.Value = UserManager.GetReviewerHistoryByActivityTypeCount(Request.QueryString( Id),StartDate,EndDate,activity)
Dim StartTime 作为 新 TimeSpan
Dim EndTime As 新 TimeSpan
Dim totalTime As 新 TimeSpan
totalTime = Convert.ToDateTime(dt。行(dt.Rows.Count - 1 )( AddedOn))。减去(dt.Rows( 0 )( AddedOn))
Dim index As 整数 = 0
Dim filterdata As 新 Filterdata
Dim prevTime As 新 DateTime
Dim prevLocation As 整数
对于 每个行作为 DataRow 在 dt.Rows
如果 index = 0 < span class =code-keyword> AndAlso 行( CheckInLocation) IsNot DBNull.Value 然后
prevTime = row( AddedOn)
prevLocation = row( CheckInLocation )
Else
如果 prevLocation = row( CheckInLocation)然后
Dim timeDiff As TimeSpan = Convert.ToDateTime(row( AddedOn))。Subtract(prevTime)
EndTime = EndTime.Add(timeD iff)
prevLocation = IIf(row( LocationAddress) DBNull.Value, 0 ,row( LocationAddress))
prevTime = row( AddedOn)
其他
结束 如果
结束 如果
index + = 1
下一页
rptreviewerhistory.DataSource = filterlist
rptreviewerhistory.DataBind()
结束 如果
结束 Sub
公开 类 Filterdata
公开 属性 customdate As DateTime
公共 属性 fromdate As DateTime
Public 属性 todate As DateTime
Public 属性 timespent 作为 DateTime
公开 属性 LocationName 作为 Strin g
结束 类
解决方案
如果要从数据库中提取数据,则可以使用SQL GROUP BY [ ^ ]。如果你想在代码中这样做,那么你可以使用Linq的 GROUP BY [ ^ ]子句。
试试这个:
' 需要将字符串转换为日期
Dim provider As System.Globalization。 CultureInfo = System.Globalization.CultureInfo.InvariantCulture
' create sample datatable
< span class =code-keyword> Dim dt As DataTable = 新 DataTable()
dt.Columns.Add( New DataColumn( Id,输入。 GetType ( System.Int32)))
dt .Columns.Add( New DataColumn( LocationId,键入。 GetType ( System.Int32 )))
dt.Columns.Add( New DataColumn( UserName,Type。 GetType ( System.String)))
dt.Columns.Add( New DataColumn( Startdate,Type。 GetType ( System.DateTime)))
' 插入数据
dt.Rows.Add(新 对象(){ 1 , 10 , xz,DateTime.ParseExact( 2017-02 -21 09:05:20, yyyy-MM-dd HH:mm:ss,provider)})
dt.Rows.Add( New Object () { 2 , 10 , xz,DateTime.ParseExact( 2017-02-21 09 :15:20, yyyy-MM-dd HH:mm:ss ,提供者)})
dt.Rows.Add(新 对象(){ 3 , 10 , xz,DateTime.ParseExact( < span class =code-string> 2017-02-21 09:25:20, yyyy-MM-dd HH:mm:ss,provider)})
dt.Rows.Add( New 对象(){ 4 , 10 , xz,DateTime.ParseExact( 2017-02-21 09:35:20, yyyy-MM -dd HH:mm:ss,provider)})
dt.Rows.Add( New 对象(){ 5 , 11 , xz,DateTime.ParseExact( 2017-02-21 09:45:20, yyyy-MM-dd HH:mm:ss,provider)})
dt.Rows.Add( New 对象(){ 6 , 11 , xz,DateTime.ParseExact( 2017-02-21 09:55:20, yyyy-MM-dd HH:mm:ss,provider)})
dt.Rows.Add( New 对象(){ 7 , 11 , xz,DateTime.ParseExact( 2017-02-21 10:05:20, yyyy-MM-dd HH:mm:ss,provider)})
dt.Rows.Add(新 对象(){ 8 , 11 , xz,DateTime.ParseExact( 2017-02-21 10:15:20, yyyy-MM-dd HH:mm:ss,provider)})
' 按LocationId和UserName分组数据
' 计算时差
Dim result = dt.AsEnumerable()_
.GroupBy(功能(x)新 使用 {_
.LocationId = x.Field( Of 整数)( LocationId ),_
.UserName = x.Field( Of String )( UserName)})_
。选择(功能(grp)新 使用 _
{_
.LocationId = grp.Key.LocationId,_
.UserName = grp.Key.UserName,_
.StartTime = grp。选择(功能(a)a.Field( of DateTime)( StartDate))。Min()。ToString( HH:mm:ss),_
.EndTime = grp。选择(功能(a)a.Field( of DateTime)( StartDate))。Max()。ToString( HH:mm:ss),_
.TimeDiff = grp。选择( 功能(a)a.Field( of DateTime)( StartDate))。Max() - grp。选择(功能(a)a.Field( of DateTime)( StartDate))。Min()_
})_
.ToList()
以上应该返回:
LocationId UserName StartTime EndTime TimeDiff
10 xz 09:05:20 09:35:20 00:30:00
11 xz 09:45:20 10:15:20 00:30:00
I have datatable in the following format
Id LocationId UserName Startdate
1 10 xz 2017-02-21 09:05:20
2 10 xz 2017-02-21 09:15:20
3 10 xz 2017-02-21 09:25:20
4 10 xz 2017-02-21 09:35:20
5 11 xz 2017-02-21 09:45:20
6 11 xz 2017-02-21 09:55:20
7 11 xz 2017-02-21 10:05:20
8 11 xz 2017-02-21 10:15:20
I want to calculate starttime end time and differnce of time ,
and have show record as
Id locationId startdate starttime endtime timespan
1 10 2017-02-21 09:05 09:35:20 0hr 30min0sec
1 10 2017-02-21 09:05 09:35:20 0hr 30min0sec
What I have tried:
<pre> If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
hdnreviewerHistoryCount.Value = UserManager.GetReviewerHistoryByActivityTypeCount(Request.QueryString("Id"), StartDate, EndDate, activity)
Dim StartTime As New TimeSpan
Dim EndTime As New TimeSpan
Dim totalTime As New TimeSpan
totalTime = Convert.ToDateTime(dt.Rows(dt.Rows.Count - 1)("AddedOn")).Subtract(dt.Rows(0)("AddedOn"))
Dim index As Integer = 0
Dim filterdata As New Filterdata
Dim prevTime As New DateTime
Dim prevLocation As Integer
For Each row As DataRow In dt.Rows
If index = 0 AndAlso row("CheckInLocation") IsNot DBNull.Value Then
prevTime = row("AddedOn")
prevLocation = row("CheckInLocation")
Else
If prevLocation = row("CheckInLocation") Then
Dim timeDiff As TimeSpan = Convert.ToDateTime(row("AddedOn")).Subtract(prevTime)
EndTime = EndTime.Add(timeDiff)
prevLocation = IIf(row("LocationAddress") Is DBNull.Value, 0, row("LocationAddress"))
prevTime = row("AddedOn")
Else
End If
End If
index += 1
Next
rptreviewerhistory.DataSource = filterlist
rptreviewerhistory.DataBind()
End If
End Sub
Public Class Filterdata
Public Property customdate As DateTime
Public Property fromdate As DateTime
Public Property todate As DateTime
Public Property timespent As DateTime
Public Property LocationName As String
End Class
解决方案
If you are pulling data from a database, then you can use the SQL GROUP BY[^]. If you want to do it in code, then you could use Linq's GROUP BY[^] clause.
Try this:
'needed to convert string into date Dim provider As System.Globalization.CultureInfo = System.Globalization.CultureInfo.InvariantCulture 'create sample datatable Dim dt As DataTable = New DataTable() dt.Columns.Add(New DataColumn("Id", Type.GetType("System.Int32"))) dt.Columns.Add(New DataColumn("LocationId", Type.GetType("System.Int32"))) dt.Columns.Add(New DataColumn("UserName", Type.GetType("System.String"))) dt.Columns.Add(New DataColumn("Startdate", Type.GetType("System.DateTime"))) 'insert data dt.Rows.Add(New Object(){1, 10, "xz", DateTime.ParseExact("2017-02-21 09:05:20", "yyyy-MM-dd HH:mm:ss", provider)}) dt.Rows.Add(New Object(){2, 10, "xz", DateTime.ParseExact("2017-02-21 09:15:20", "yyyy-MM-dd HH:mm:ss", provider)}) dt.Rows.Add(New Object(){3, 10, "xz", DateTime.ParseExact("2017-02-21 09:25:20", "yyyy-MM-dd HH:mm:ss", provider)}) dt.Rows.Add(New Object(){4, 10, "xz", DateTime.ParseExact("2017-02-21 09:35:20", "yyyy-MM-dd HH:mm:ss", provider)}) dt.Rows.Add(New Object(){5, 11, "xz", DateTime.ParseExact("2017-02-21 09:45:20", "yyyy-MM-dd HH:mm:ss", provider)}) dt.Rows.Add(New Object(){6, 11, "xz", DateTime.ParseExact("2017-02-21 09:55:20", "yyyy-MM-dd HH:mm:ss", provider)}) dt.Rows.Add(New Object(){7, 11, "xz", DateTime.ParseExact("2017-02-21 10:05:20", "yyyy-MM-dd HH:mm:ss", provider)}) dt.Rows.Add(New Object(){8, 11, "xz", DateTime.ParseExact("2017-02-21 10:15:20", "yyyy-MM-dd HH:mm:ss", provider)}) 'group data by LocationId and UserName 'calculate time difference Dim result = dt.AsEnumerable() _ .GroupBy(Function(x) New With { _ .LocationId=x.Field(Of Integer)("LocationId"), _ .UserName=x.Field(Of String)("UserName")}) _ .Select(Function(grp) New With _ { _ .LocationId = grp.Key.LocationId, _ .UserName = grp.Key.UserName, _ .StartTime = grp.Select(Function(a) a.Field(Of DateTime)("StartDate")).Min().ToString("HH:mm:ss"), _ .EndTime = grp.Select(Function(a) a.Field(Of DateTime)("StartDate")).Max().ToString("HH:mm:ss"), _ .TimeDiff = grp.Select(Function(a) a.Field(Of DateTime)("StartDate")).Max() - grp.Select(Function(a) a.Field(Of DateTime)("StartDate")).Min() _ }) _ .ToList()
Above should return:
LocationId UserName StartTime EndTime TimeDiff 10 xz 09:05:20 09:35:20 00:30:00 11 xz 09:45:20 10:15:20 00:30:00
这篇关于如何在以下情况下查询groupby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文