查询通用列表
Query Generic List
我必须使用 VendorID 登录 FTP activity。
有些供应商有多个FTP个用户名,但我只需要登录一个(最新的)。
例如:
- VendorID 1(两个用户名)
- 麦克
皮特
VendorID 4(两个用户名)
- 杰克
- 路易斯
所以我创建了一个 class 来存储 FTP activity:
Public Class VendorClass
Public VendorID As Integer
Public FTPUserName As String
Public File As String
Public Date As DateTime
End Class
- 我将文件加载到 VendorClass 列表中
问题:
如何按UserID查询列表和分组,return最新日期和时间。
原列表
VendorID FTP UserName File Date
1 Mike BalanceSheet.csv 6/17/2015 10:20
1 Pete BalanceSheet.csv 6/17/2015 10:40
2 Ron BalanceSheet.csv 6/17/2015 10:20
3 Julia BalanceSheet.csv 6/17/2015 14:20:02
4 Jack BalanceSheet.csv 6/17/2015 11:20
4 Luis BalanceSheet.csv 6/17/2015 09:20
分组结果:
VendorID FTP UserName File Date
1 Pete BalanceSheet.csv 6/17/2015 10:40
2 Ron BalanceSheet.csv 6/17/2015 10:20
3 Julia BalanceSheet.csv 6/17/2015 14:20:02
4 Jack BalanceSheet.csv 6/17/2015 11:20
我的功能当时只能select 1 个供应商:
Private Function FlattenDuplicateEntries(ActivityList As List(Of VendorClass)) As List(Of VendorClass)
FlattenDuplicateEntries = New List(Of VendorClass)
For Each vendor In FTPUserNameList
Dim val As New VendorClass
val = ActivityList.Find(Function(value As VendorClass) value.VendorID = vendor.VendorID)
Next
End Function
Private Function FlattenDuplicateEntries(ActivityList As List(Of VendorClass)) As List(Of VendorClass)
Dim filtered As New Dictionary(Of Integer, VendorClass)()
For Each vendor In ActivityList
If Not filtered.ContainsKey(vendor.VendorID) Then
filtered.Add(vendor.VendorID, vendor)
Else If filtered(vendor.VendorID).Date < vendor.Date Then
filtered(vendor.VendorID) = vendor
End If
Next
Return filtered.Values.ToList()
End Function
(因为这也被标记为 C#,我不会尝试翻译成 VB。)
类似于:
var lastByVendor = from act in ActivityList
group by act.VendorID into grp
let last = grp.OrderByDescending(x => x.Date).First()
select new {
VendorId = grp.Key,
Date = last.Date,
File = last.File,
FTPUserName = last.FTPUserName
};
在此我:
- 按供应商 ID 分组,然后为每个供应商完成其余工作
- 获取最后一条记录(BCL中没有
MaxBy
运算符,所以使用排序)
- 组的
Key
是来自组 的值
我必须使用 VendorID 登录 FTP activity。 有些供应商有多个FTP个用户名,但我只需要登录一个(最新的)。
例如:
- VendorID 1(两个用户名)
- 麦克
皮特
VendorID 4(两个用户名)
- 杰克
- 路易斯
所以我创建了一个 class 来存储 FTP activity:
Public Class VendorClass
Public VendorID As Integer
Public FTPUserName As String
Public File As String
Public Date As DateTime
End Class
- 我将文件加载到 VendorClass 列表中
问题:
如何按UserID查询列表和分组,return最新日期和时间。
原列表
VendorID FTP UserName File Date
1 Mike BalanceSheet.csv 6/17/2015 10:20
1 Pete BalanceSheet.csv 6/17/2015 10:40
2 Ron BalanceSheet.csv 6/17/2015 10:20
3 Julia BalanceSheet.csv 6/17/2015 14:20:02
4 Jack BalanceSheet.csv 6/17/2015 11:20
4 Luis BalanceSheet.csv 6/17/2015 09:20
分组结果:
VendorID FTP UserName File Date
1 Pete BalanceSheet.csv 6/17/2015 10:40
2 Ron BalanceSheet.csv 6/17/2015 10:20
3 Julia BalanceSheet.csv 6/17/2015 14:20:02
4 Jack BalanceSheet.csv 6/17/2015 11:20
我的功能当时只能select 1 个供应商:
Private Function FlattenDuplicateEntries(ActivityList As List(Of VendorClass)) As List(Of VendorClass)
FlattenDuplicateEntries = New List(Of VendorClass)
For Each vendor In FTPUserNameList
Dim val As New VendorClass
val = ActivityList.Find(Function(value As VendorClass) value.VendorID = vendor.VendorID)
Next
End Function
Private Function FlattenDuplicateEntries(ActivityList As List(Of VendorClass)) As List(Of VendorClass)
Dim filtered As New Dictionary(Of Integer, VendorClass)()
For Each vendor In ActivityList
If Not filtered.ContainsKey(vendor.VendorID) Then
filtered.Add(vendor.VendorID, vendor)
Else If filtered(vendor.VendorID).Date < vendor.Date Then
filtered(vendor.VendorID) = vendor
End If
Next
Return filtered.Values.ToList()
End Function
(因为这也被标记为 C#,我不会尝试翻译成 VB。)
类似于:
var lastByVendor = from act in ActivityList
group by act.VendorID into grp
let last = grp.OrderByDescending(x => x.Date).First()
select new {
VendorId = grp.Key,
Date = last.Date,
File = last.File,
FTPUserName = last.FTPUserName
};
在此我:
- 按供应商 ID 分组,然后为每个供应商完成其余工作
- 获取最后一条记录(BCL中没有
MaxBy
运算符,所以使用排序) - 组的
Key
是来自组 的值