好吧,我确定这对某人来说很简单,但它开始让我撕掉我的听力,所以如果这看起来像一个简单的问题,我会事先道歉:
我有以下课程:
Public Class Ticket
Public Property ticket_id As String
Public Property ticket_assignee As String
Public Property ticket_email As String
End Class
我正在尝试使用LINQ按ticket_email
字段对列表进行分组,但允许我获取每个组的ticket_assignee
字段和计数。
例如:
对于每个电子邮件地址,请返回受让人姓名以及该电子邮件组中的票数,如下所示:
'fred@someplace.com' = Fred, 4
'joe@someplace.com' = Joe, 2
'bob@someplace.com' = Bob, 8
等...
我仍然试图了解LINQ,并希望更多地了解它。任何帮助将不胜感激。
答案 0 :(得分:0)
Dim tickets As New List(Of Ticket)
For i = 1 To 3
tickets.Add(New Ticket With {.ticket_id = i.ToString, .ticket_email = "a", .ticket_assignee = "me"})
Next
For i = 4 To 6
tickets.Add(New Ticket With {.ticket_id = i.ToString, .ticket_email = "b", .ticket_assignee = "me2"})
Next
Dim group = tickets.GroupBy(Function(x) x.ticket_assignee).Select(Function(y) New Tuple(Of String, Integer)(y.First.ticket_assignee, y.Count))
答案 1 :(得分:0)
查询语法解决方案:
Sub Main
Dim q = from entry in Ticket.GetTickets
group entry by entry.ticket_assignee Into NewGroup = Group, Count()
select new with{
.Assignee = NewGroup.Select(function(x) x.ticket_assignee).First,
.Email = NewGroup.Select(function(x) x.ticket_email).First,
.Count = Count}
'q.dump()
End Sub
' Define other methods and classes here
Public Class Ticket
Public Property ticket_id As Integer
Public Property ticket_assignee As String
Public Property ticket_email As String
Public Shared Function GetTickets as list(of Ticket)
Return New List(of Ticket) From {New Ticket With {.ticket_id = 1, .ticket_assignee = "Fred", .ticket_email = "fred@ticket.de"},
New Ticket With {.ticket_id = 2, .ticket_assignee = "Fred", .ticket_email = "fred@ticket.de"},
New Ticket With {.ticket_id = 3, .ticket_assignee = "Fred", .ticket_email = "fred@ticket.de"},
New Ticket With {.ticket_id = 4, .ticket_assignee = "Anna", .ticket_email = "Anna@ticket.de"},
New Ticket With {.ticket_id = 5, .ticket_assignee = "Anna", .ticket_email = "Anna@ticket.de"},
New Ticket With {.ticket_id = 6, .ticket_assignee = "Jack", .ticket_email = "Jack@ticket.de"}}
End Function
End Class
给你:
为了学习Linq,我强烈推荐LinqPad。您可以选择VB.NET程序,复制我的代码,取消注释.dump()行并运行它。