VB Syntax for LINQ query that creates a new{} set?
I am trying to AVERAGE 6 different fields on a DataTable, but wit开发者_JAVA百科hout grouping. Just the AVERAGE. I have found a couple of examples in C#, but can't find any examples for how to do this in VB. Can someone please help me convert the syntax?
Here is what I have so far:
Dim query = From dtRow In dtIn.AsEnumerable _
Where dtRow.Field(Of String)("FOLLOWUP") = "Alert" _
Select New With { _
.Brand_Functional_Avg = XXX.Average(Function(f) f("Brand_Functional")), _
.Brand_Personal_Avg = XXX.Average(Function(f) f("Brand_Personal")) _
}
What should I use for XXX? I tried all of the options that I could think of and nothing is compiling.
Trust me, if I could write this in C#, I would, but the project requires VB.
If you're trying to find the average, you shouldn't be using a select to start with - that's going to create a new result for each row, whereas you want just two results for the whole table. I suggest you do something like:
Dim filtered = From dtRow in dtIn.AsEnumerable _
Where dtRow.Field(Of String)("FOLLOWUP") = "Alert"
Dim functionalAvg = filtered.Average(Function(f) f("Brand_Functional"))
Dim personalAvg = filtered.Average(Function(f) f("Brand_Personal"))
精彩评论