HowTo? LINQ Query Table Name (Range Variable) Not Known Until Runtime

HowTo? LINQ Query Table Name (Range Variable) Not Known Until Runtime

am 23.04.2008 16:37:11 von joey.powell

I am querying a DataSet with LINQ. I am running into a problem when
trying to construct my query because in the "from" clause I do not
know the table name (range variable) until runtime. Possible table
names might include "SomeTable1" or "SomeTable236" etc...
Unfortunately when I try to use something like "from SomeTable +
MyChangingNumber.ToString() in..." in my from clause it does not work.
How can I set this up to use a range variable whose value is dynamic /
not known until runtime?

Re: HowTo? LINQ Query Table Name (Range Variable) Not Known Until Runtime

am 23.04.2008 17:04:01 von Patrice

You could try the dynamic LINQ library (but I never used it and I'm not sure
it covers this scenario) :
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-li nq-part-1-using-the-linq-dynamic-query-library.aspx

Which programming language are you using ? You could likely use Reflection
in C# or CallByName in VB.NET (I tried this later method once) to
dynamically call the appropriate member on the DataContext.

I understand you may have some reasons for this design but I'm not a big fan
of those "select by table name instead of using a where clause" design and
it could be likely a subject of its own...

Finally you'll find a LINQ specific forum at :
http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=123& SiteID=1

--
Patrice

"Joey" a écrit dans le message de groupe de
discussion :
220288e6-a8c6-4964-afd7-339db08922b2@k37g2000hsf.googlegroup s.com...
> I am querying a DataSet with LINQ. I am running into a problem when
> trying to construct my query because in the "from" clause I do not
> know the table name (range variable) until runtime. Possible table
> names might include "SomeTable1" or "SomeTable236" etc...
> Unfortunately when I try to use something like "from SomeTable +
> MyChangingNumber.ToString() in..." in my from clause it does not work.
> How can I set this up to use a range variable whose value is dynamic /
> not known until runtime?

RE: HowTo? LINQ Query Table Name (Range Variable) Not Known Until Runt

am 23.04.2008 18:33:13 von brucebarker

Linq does not support this. the lambda expression that represents the table
name is converted to an expression tree at compile time (so no variables
allowed). you can write you own method to create the expression tree at
runtime, and then construct the linq query.


-- bruce (sqlwork.com)


"Joey" wrote:

> I am querying a DataSet with LINQ. I am running into a problem when
> trying to construct my query because in the "from" clause I do not
> know the table name (range variable) until runtime. Possible table
> names might include "SomeTable1" or "SomeTable236" etc...
> Unfortunately when I try to use something like "from SomeTable +
> MyChangingNumber.ToString() in..." in my from clause it does not work.
> How can I set this up to use a range variable whose value is dynamic /
> not known until runtime?
>