I’m looking for a way to return a name value pair list directly from a Linq to SQL query without having to loop through the results like I’m doing in the code below:
Public Shared Function GetUserTransactionCounts(fromDate As DateTime, toDate As DateTime) As List(Of KeyValuePair(Of String, Integer))
Using dc As New ProntoDataContext()
Dim users As IEnumerable(Of User) = From u In dc.Users Where u.CreatedTransactions.Any(Function(t) t.TSCreate >= fromDate And t.TSCreate <= toDate)
Dim list As New List(Of KeyValuePair(Of String, Integer))
For Each user As User In users
Dim item As New KeyValuePair(Of String, Integer)(user.CommonName, user.CreatedTransactions.Count)
list.Add(item)
Next
Return list
End Using
End Function
Try converting to
IEnumerable, and then usingSelect:EDIT : If you would like to make an
IEnumerable, remove theToList:This will not run the query until the
enumerableis enumerated for the first time.