我必须使用VendorID记录FTP活动。 有些供应商有多个FTP用户名,但我只需要记录一个(最新的)。
例如:
Pete
VendorID 4(两个用户名)
所以我创建了一个存储FTP活动的类:
Public Class VendorClass
Public VendorID As Integer
Public FTPUserName As String
Public File As String
Public Date As DateTime
End Class
问题:
如何按UserID查询列表和组,并返回最新的日期和时间。
原始列表
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
我的功能当时只能选择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
答案 0 :(得分:1)
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
答案 1 :(得分:0)
(因为这也标记为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
};
在此我:
MaxBy
运算符,因此使用排序)Key
是