在 LINQ select 查询上调用 ToArray
call ToArray on LINQ select query
我有这个查询:
Dim test = result.GroupBy(Function(row) groupedindexes.Select(
Function(grpindex) row(grpindex)).ToArray, comp)
我正在构建表达式树。我已经在 GroupBy
函数中构建了该部分,现在我想调用 ToArray
方法。这是代码:
Public Function Grouping(ByVal result As IEnumerable(Of Object()), ByVal groupedindexes As List(Of Integer), ByVal comparer As compare) As Expression
Dim groupbyMethod = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(Function(m) m.Name = "GroupBy").MakeGenericMethod(GetType(Object()), GetType(System.Collections.Generic.IEqualityComparer(Of Object())))
Dim convertMethod As MethodInfo = Nothing
Dim rowParameter = Expression.Parameter(GetType(Object()), "Row")
Dim indexParameter = Expression.Parameter(GetType(Integer), "grpindex")
Dim methodstring As String
Dim expr As Expression = Nothing
Dim index As Integer
Dim grpindexes As Expression = Expression.Constant(groupedindexes, GetType(System.Collections.Generic.List(Of Integer)))
Dim selectMethod = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(Function(m) m.Name = "Select").MakeGenericMethod(GetType(Integer), GetType(Object))
Dim toarrayMethod2 = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(Function(m) m.Name = "ToArray").MakeGenericMethod(GetType(Object))
Dim cmp As Expression = Expression.Constant(comparer, GetType(compare))
Dim fieldselector As Expressions.LambdaExpression
fieldselector = Expression.Lambda(Expression.ArrayAccess(rowParameter, indexParameter), indexParameter)
Dim outerfieldselector As Expressions.LambdaExpression
outerfieldselector = Expression.Lambda(Expression.Call(selectMethod, grpindexes, fieldselector), rowParameter)
expr = Expression.Call(groupbyMethod, Expression.Call(outerfieldselector, toarrayMethod2), cmp)
Return expr
End Function
我在 expr = ...
行收到一条错误消息:静态方法需要空实例,非静态方法需要非空实例。
我已经知道这个错误消息,但我认为,表达式调用是正确的:我在 outerfieldselector
.
上调用了 ToArray
方法
你能帮帮我吗?您可以复制并粘贴代码并进行测试。如有必要,您可以删除比较 class。
谢谢。
你有几个错误:
- 您使用的
GroupBy
方法有误。
- 调用像
GroupBy
和 ToArray
这样的静态方法需要一个空实例,就像错误所说的那样。
这是更正后的代码。更改的行是以 expr =
和 outerfieldselector =
开头并获取 GroupBy
方法的行:
Public Function Grouping(ByVal result As IEnumerable(Of Object()), ByVal groupedindexes As List(Of Integer), ByVal comparer As compare) As Expression
Dim groupbyMethod = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).
Where(Function(m) m.Name = "GroupBy").
First(Function(m) m.GetParameters().Count() = 3).
MakeGenericMethod(GetType(Object()), GetType(Object()))
Dim convertMethod As MethodInfo = Nothing
Dim rowParameter = Expression.Parameter(GetType(Object()), "Row")
Dim indexParameter = Expression.Parameter(GetType(Integer), "grpindex")
Dim methodstring As String
Dim expr As Expression = Nothing
Dim index As Integer
Dim grpindexes As Expression = Expression.Constant(groupedindexes, GetType(System.Collections.Generic.List(Of Integer)))
Dim selectMethod = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(Function(m) m.Name = "Select").MakeGenericMethod(GetType(Integer), GetType(Object))
Dim toarrayMethod2 = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(Function(m) m.Name = "ToArray").MakeGenericMethod(GetType(Object))
Dim cmp As Expression = Expression.Constant(comparer, GetType(compare))
Dim fieldselector As Expressions.LambdaExpression
fieldselector = Expression.Lambda(Expression.ArrayAccess(rowParameter, indexParameter), indexParameter)
Dim outerfieldselector As Expressions.LambdaExpression
outerfieldselector = Expression.Lambda(Expression.Call(Nothing, toarrayMethod2, Expression.Call(selectMethod, grpindexes, fieldselector)), rowParameter)
Dim test = Enumerable.GroupBy(result, Function(row) groupedindexes.Select(Function(grpindex) row(grpindex)).ToArray(), comparer)
expr = Expression.Call(Nothing, groupbyMethod, Expression.Constant(result), outerfieldselector, Expression.Constant(comparer))
Return expr
End Function
我有这个查询:
Dim test = result.GroupBy(Function(row) groupedindexes.Select(
Function(grpindex) row(grpindex)).ToArray, comp)
我正在构建表达式树。我已经在 GroupBy
函数中构建了该部分,现在我想调用 ToArray
方法。这是代码:
Public Function Grouping(ByVal result As IEnumerable(Of Object()), ByVal groupedindexes As List(Of Integer), ByVal comparer As compare) As Expression
Dim groupbyMethod = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(Function(m) m.Name = "GroupBy").MakeGenericMethod(GetType(Object()), GetType(System.Collections.Generic.IEqualityComparer(Of Object())))
Dim convertMethod As MethodInfo = Nothing
Dim rowParameter = Expression.Parameter(GetType(Object()), "Row")
Dim indexParameter = Expression.Parameter(GetType(Integer), "grpindex")
Dim methodstring As String
Dim expr As Expression = Nothing
Dim index As Integer
Dim grpindexes As Expression = Expression.Constant(groupedindexes, GetType(System.Collections.Generic.List(Of Integer)))
Dim selectMethod = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(Function(m) m.Name = "Select").MakeGenericMethod(GetType(Integer), GetType(Object))
Dim toarrayMethod2 = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(Function(m) m.Name = "ToArray").MakeGenericMethod(GetType(Object))
Dim cmp As Expression = Expression.Constant(comparer, GetType(compare))
Dim fieldselector As Expressions.LambdaExpression
fieldselector = Expression.Lambda(Expression.ArrayAccess(rowParameter, indexParameter), indexParameter)
Dim outerfieldselector As Expressions.LambdaExpression
outerfieldselector = Expression.Lambda(Expression.Call(selectMethod, grpindexes, fieldselector), rowParameter)
expr = Expression.Call(groupbyMethod, Expression.Call(outerfieldselector, toarrayMethod2), cmp)
Return expr
End Function
我在 expr = ...
行收到一条错误消息:静态方法需要空实例,非静态方法需要非空实例。
我已经知道这个错误消息,但我认为,表达式调用是正确的:我在 outerfieldselector
.
ToArray
方法
你能帮帮我吗?您可以复制并粘贴代码并进行测试。如有必要,您可以删除比较 class。
谢谢。
你有几个错误:
- 您使用的
GroupBy
方法有误。 - 调用像
GroupBy
和ToArray
这样的静态方法需要一个空实例,就像错误所说的那样。
这是更正后的代码。更改的行是以 expr =
和 outerfieldselector =
开头并获取 GroupBy
方法的行:
Public Function Grouping(ByVal result As IEnumerable(Of Object()), ByVal groupedindexes As List(Of Integer), ByVal comparer As compare) As Expression
Dim groupbyMethod = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).
Where(Function(m) m.Name = "GroupBy").
First(Function(m) m.GetParameters().Count() = 3).
MakeGenericMethod(GetType(Object()), GetType(Object()))
Dim convertMethod As MethodInfo = Nothing
Dim rowParameter = Expression.Parameter(GetType(Object()), "Row")
Dim indexParameter = Expression.Parameter(GetType(Integer), "grpindex")
Dim methodstring As String
Dim expr As Expression = Nothing
Dim index As Integer
Dim grpindexes As Expression = Expression.Constant(groupedindexes, GetType(System.Collections.Generic.List(Of Integer)))
Dim selectMethod = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(Function(m) m.Name = "Select").MakeGenericMethod(GetType(Integer), GetType(Object))
Dim toarrayMethod2 = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(Function(m) m.Name = "ToArray").MakeGenericMethod(GetType(Object))
Dim cmp As Expression = Expression.Constant(comparer, GetType(compare))
Dim fieldselector As Expressions.LambdaExpression
fieldselector = Expression.Lambda(Expression.ArrayAccess(rowParameter, indexParameter), indexParameter)
Dim outerfieldselector As Expressions.LambdaExpression
outerfieldselector = Expression.Lambda(Expression.Call(Nothing, toarrayMethod2, Expression.Call(selectMethod, grpindexes, fieldselector)), rowParameter)
Dim test = Enumerable.GroupBy(result, Function(row) groupedindexes.Select(Function(grpindex) row(grpindex)).ToArray(), comparer)
expr = Expression.Call(Nothing, groupbyMethod, Expression.Constant(result), outerfieldselector, Expression.Constant(comparer))
Return expr
End Function