看完this post我意识到我不能将匿名类型作为参数传递给函数。那么我还有哪些其他选择?
也许,将 Query.ToList
作为参数传递会有所帮助,还是我在重新发明轮子?
更新:我有以下查询,我想将其传递给一个函数:
Sub Test
Dim Query = (From c In DB Select New With { .ElementName = c.Name })
DoSomething(Query)
End sub
Private Function DoSomething(ByVal Q as object) as string
Dim Query = From c In Q Select c
End Function
我得到的错误是
Expression of type 'Object' is not queryable
最佳答案
After reading this post i realized that i cannot pass an anonymous type as a parameter to a function. So what other options do i have?
帖子在撒谎。当然,您可以将匿名类型传递给方法。
Function DoSomething(Of T)(items As IEnumerable(Of T))
…
End Function
Dim Query = (From c In DB Select New With {.ElementName = c.Name})
DoSomething(Query)
无论哪种情况,您对 DoSomething
的定义都是问题所在,因为 Object
当然不是可查询的对象。 ToList
根本没有帮助,因为结果仍然是匿名类型的集合。
关于c# - 我可以将匿名类型作为参数传递给函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6895145/