我有一个静态列表,我想在最后打印。
class User
{
int ID {get;set;}
string status {get; set;}
DateTime startTime {get;set;}
int groupId {get;set;}
}
我想在内存中维护用户列表并在最后打印。 现在列表可以包含以下数据:
List<User> userList = new List<User>();
userList.Add( new User{ ID = 1, ..., groupId = 1 } );
userList.Add( new User{ ID = 2, ..., groupId = 1 } );
userList.Add( new User{ ID = 3, ..., groupId = 2 } );
userList.Add( new User{ ID = 4, ..., groupId = 1 } );
userList.Add( new User{ ID = 5, ..., groupId = 3 } );
我想对这些数据进行分组,我可以使用userList.GroupBy(x => x.groupID)
进行分组,得到Enumberable<ID, List<User>>()
。对不起,在分组列表中我想检查是否有任何用户状态==失败,然后我想打印所有用户信息,如果status == true,那么我想只打印userId或者名。为此,我正在做
groupedList.Any(x => x.status == fail)
但我希望我的输出按startTime排序,然后我希望结果按Id
分组var result = userList.GroupBy(x=> x.groupID).OrderByDescending(x => x.startTime).ToList();
然后,当我像
那样打印时,我想要我的结果startTime - 10.00 groupId - 1 ,id - 1, ....status - fail ... more properties
startTime - 10.00 groupId - 1, id - 2, .... status - pass ...
startTIme - 10.00 groupId - 1, id - 4, status - pass
startTiem = 10.00 groupId - 2, id - 3, status - pass (just this info)
starttime - 9.50 groupId - 3, id - 5, status - fail ... more info
starttime - 9.50 groupid - 3, id - 6, status - pass ... (still more info. as group has a fail case)
如果我使用上面的linq查询,它不会提供相同的输出。请告知如何实现这一目标。
答案 0 :(得分:2)
在我看来,你根本不想分组 - 你的列表中有5个元素和你的输出。您只想按多个属性订购。这应该有用,然后:
Sub unique_column()
Dim data() As Variant 'array that will store all of the unique letters
c = 1
Range("A1").Select
Do While ActiveCell.Value <> ""
ReDim Preserve data(1 To c) As Variant
If IsInArray(ActiveCell.Value, data()) = False Then 'we are on a new unique letter and will add it to the array
data(c) = ActiveCell.Value
c = c + 1
End If
ActiveCell.Offset(1, 0).Select
Loop
'now we can spit out the letters in the array into a new column
Range("B1").Value = "Unique letters:"
Dim x As Variant
Range("B2").Select
For Each x In data()
ActiveCell.Value = x
ActiveCell.Offset(1, 0).Select
Next x
Range("A1").Select
c = c - 1
killer = MsgBox("Processing complete!" & vbNewLine & c & "unique letters applied.", vbOKOnly)
End Sub
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function