2

I'm working with LINQ to SQL with ASP.NET 4 and C#. I've created a LINQ query that results in a new LIST (ie. ToList()). The returned LIST is of an ANONYMOUS type, because it's being created dynamically by the query. I want to be able to declare a variable at the page level in the code behind so I can use it in other functions and also on the front page using '<%= %>'.

Visual Studio tells me that the result is of an anonymous type, but gives me the classes names, for example: {CLASS1, CLASS2}.

How can I declare the variable of this anonymous type?

4
  • 8
    If you're not planning on immediately using the results of a query that spits out a sequence of anonymous types, you should strongly consider declaring an actual type to store the data. Commented Aug 9, 2011 at 14:37
  • 3
    dlev is right. Anonymous types really aren't designed to be passed around. You're much better off declaring an actual type Commented Aug 9, 2011 at 14:38
  • Although, as everyone pointed out, what you're trying to do is an abuse of anonymous types, there's technically a way to cast anonymous types by example. Commented Aug 9, 2011 at 15:38
  • possible duplicate of Declaration of Anonymous types List Commented Jun 28, 2014 at 8:44

3 Answers 3

1

Anonymous types, by their very nature, can't be explicitly "declared". They're anonymous, unknown; you don't have a type to declare. As such, anonymous types, whether directly or as a generic type parameter, cannot be specified as parameters, used as a return type, or stored to any explicitly typed variable. You must use var to assign them, and that means you're pretty much limited to using the type in local scope.

Your only option, basically, is to convert your anonymous type into a collected type that can be explicitly declared (i.e. you must declare the type itself). Just create a simple DTO class, and before calling ToList, run the anonymous type through a Select() Linq node that uses the anonymous type to initialize your strong class.

If you are using .NET 4.0, you COULD go dynamic. The dynamic keyword, and any object that's been passed through a parameter or return type declared dynamic, basically tells the compiler not to try to verify any operation performed on it; you can try to use any operator, call any method, etc. HOWEVER, if the operator, member, or whatever other operation you are performing isn't valid for what the object actually is behind the scenes, you'll get runtime exceptions which you must handle very robustly. For this reason, the dynamic feature of .NET 4.0 is not to be used lightly.

The only other thing you can do, as Kratz said, is return an ArrayList, or an Object[] array, and use reflection to discover the anonymous type's structure. It's a no-no pretty much any way you slice it; you lose the strong typing of the collection, your code bloats considerably as you need several lines of code to perform even the simplest get or set operations, and reflection, again by its nature, can be up to 100x slower than the equivalent operation on a known static type.

Sign up to request clarification or add additional context in comments.

Comments

1

You'll have to declare a type for the result, since there is no way I know of to outright declare an instance of an anonymous type. Even if you store it in a value, you would not be able to use it outside the scope where it was created.

You possible could use reflection to get at the values and just declare the variable as type IList, but that would be much more work than just creating a new type.

6 Comments

I know the classes names that are returned from the LINQ query, how can I used this? - I can't create a LIST with two params, for example <CLASS 1, CLASS 2>?!
You have to create a new class that has two properties of type Class2 and Class1, then give it a simple constructor. In your linq query do SELECT New Class3(obj1, obj2), and give your list type List<Class3>
How can I cast from the returned anonymous type list to List<Class3> inside my function, so List<class3> myvar will except the anonymous type LIST?
You can't, you have to SELECT a specific type in your LINQ. If you select multiple things like SELECT x.Name, x.Value that will return a anonymous type. If you SELECT x.Name, that will return a not-anonymous type. SO if you SELECT new Class3(x.obj1, x.obj2) you are only selecting one type so your list will be a real type, not an anonymous one.
is there an option to declare the 'var' as a class level variable (the var holds the data returned from the LINQ to SQL query)?
|
0

You could do a List<Object> and in your code you can test the type like :

if(myList[index].GetType() == typeof(Class1))
{
   //put your code for class1
}
else if(myList[index].GetType() == typeof(Class2))
{
   //put your code for class2
}

1 Comment

While you're technically right, I would never recommend doing this, it is a dirty solution and you'd be much better off just creating a simple class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.