Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

0 views
Skip to first unread message

Joey

unread,
Apr 23, 2008, 10:37:11 AM4/23/08
to
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?

Patrice

unread,
Apr 23, 2008, 11:04:01 AM4/23/08
to
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-linq-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" <joey....@topscene.com> a écrit dans le message de groupe de
discussion :
220288e6-a8c6-4964...@k37g2000hsf.googlegroups.com...

bruce barker

unread,
Apr 23, 2008, 12:33:13 PM4/23/08
to
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)

0 new messages