Reputation: 9959
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?
Perhaps, passing a Query.ToList
as parameter would help, or am i re-inventing the wheel?
Update: I have the following query, which i would like to pass to a function:
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
And the error i get is
Expression of type 'Object' is not queryable
Upvotes: 2
Views: 2709
Reputation: 9009
http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject.aspx
Thought I'd try out some other dynamic options, and it appears that you can pass a pure anonymous type via dynamic (i.e. in addition to the dictionary or expando options above).
[Test]
public void Test()
{
dynamic d;
int i = 20;
d = (dynamic) i;
Console.WriteLine(d);
}
[Test]
public void Test2()
{
dynamic d;
int i = 20;
d = (dynamic)new { a = 1, b = 12.2, c = "some text" };
MethodA(d);
Console.WriteLine(d);
}
public void MethodA(dynamic o)
{
}
Additional options: ExpandoObject.
Upvotes: -1
Reputation: 545488
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?
The post is telling lies. Of course you can pass anonymous types to a method.
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)
In either case, your definition of DoSomething
was the problem since of course Object
isn’t a queryable object. ToList
doesn’t help at all, since the result is still a collection of anonymous types.
Upvotes: 3