我正在创建一个程序,需要在Log_Type = I中过滤min log_time,在Log_Type = O中过滤最大logtime。需要在reportviewer中显示。谢谢你们,我不知道如何从最小和最大开始
答案 0 :(得分:2)
所以,您使用DataSet
DataSource
作为ReportViewer
从MySql
- 数据库填充的DataSet
。然后你应该首先过滤Linq-To-DataSet
或者通过sql过滤。
例如(通过Dim groups = From row In ds.Tables(0)
Let id = row.Field(Of Int32)("ID")
Let empId = row.Field(Of String)("EMP_ID").Trim()
Let logType = row.Field(Of String)("LOG_TYPE").Trim()
Let logDate = row.Field(Of Date)("LOG_DATE")
Let logTime = row.Field(Of TimeSpan)("LOG_TIME")
Let creditDate = row.Field(Of Date)("CREDIT_DATE")
Select data = New With {id, empId, logType, logDate, logTime, creditDate, row}
Group data By data.empId, data.logType Into TypeGroup = Group
Dim newDataSource As DataTable = ds.Tables(0).Clone() ' empty table with same schema '
For Each grp In groups
Dim iGroup = From data In grp.TypeGroup
Where StringComparer.OrdinalIgnoreCase.Equals("i", data.logType)
Dim min = (From data In iGroup Order By data.logTime Ascending).FirstOrDefault()
Dim max = (From data In iGroup Order By data.logTime Descending).FirstOrDefault()
If min IsNot Nothing Then
newDataSource.Rows.Add(min.id, min.empId, min.logType, min.logDate, min.logTime, min.creditDate)
End If
If max IsNot Nothing AndAlso Not Object.ReferenceEquals(min, max) Then
newDataSource.Rows.Add(max.id, max.empId, max.logType, max.logDate, max.logTime, max.creditDate)
End If
Dim oGroup = From data In grp.TypeGroup
Where StringComparer.OrdinalIgnoreCase.Equals("o", data.logType)
min = (From data In oGroup Order By data.logTime Ascending).FirstOrDefault()
max = (From data In oGroup Order By data.logTime Descending).FirstOrDefault()
If min IsNot Nothing Then
newDataSource.Rows.Add(min.id, min.empId, min.logType, min.logDate, min.logTime, min.creditDate)
End If
If max IsNot Nothing AndAlso Not Object.ReferenceEquals(min, max) Then
newDataSource.Rows.Add(max.id, max.empId, max.logType, max.logDate, max.logTime, max.creditDate)
End If
Next
ds.Tables.Clear()
ds.Tables.Add(newDataSource)
):
{{1}}