tags:

views:

76

answers:

2

Hello everybody I have a LINQ declaration like this :

var query = from foo in NHibernate_Helper.session.Linq<TheType>() select foo;

Is it possible to store TheType into a variable to dynamically define this one ?

Thank you by advance

A: 

No, generic types must be known at compile time.

Jamiec
So i have to make a method for every linq object ?
eka808
@eka808 Yes. And no. What you *really* have to do is re-think the way you are approaching the problem.
bzlm
@bzlm Can you give me more details ?
eka808
@eka808 Not really. But "method for every linq object" sounds like a hammer solution to a screwdriver problem. (I just made that metaphor up btw.)
bzlm
@bzlm Ok so have you got some examples of good practice ?
eka808
@eka808 I think you should try to understand whatknott's answer. http://stackoverflow.com/questions/3997066/define-a-list-type-into-a-variable/3997787#3997787
bzlm
love to know why this one got downvoted. Probably not the answer the OP was looking for!
Jamiec
@Jamiec - I wasn't the down-vote. However, the answer isn't *exactly* correct. You can use reflection and the `MakeGenericType` method of the `Type` class to create a generic type on the fly. I just wasn't able to work out how to use the result in a linq expression. Example http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/3fa84655-10cd-4c8f-bbb0-067b352ee0f9.
whatknott
+3  A: 

This doesn't do exactly what you asked, but can you just make your method generic?

public IEnumerable<T> GetSomething<T>()
{
    return (from foo in NHibernate_Helper.session.Linq<T>() select foo);
}
...
GetSomething<TheType>();
whatknott
How is this answering the original question - which was "Is it possible to store TheType into a variable to dynamically define this one?". You've not answered it at all!
Jamiec
@Jamiec edited for clarification
whatknott