C# Variable Question

0 views
Skip to first unread message

Greg

unread,
Nov 24, 2009, 6:10:09 AM11/24/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I have another question (a question a day keep the dumbass at bay!).

Suppose I have a string variable called TableName.
And I have three tables called Tom, Dick, Harry
I determine through code that
Now the table name can match the name of some tables I have in my
database.
How could I query that table with LINQ using the variable name?

So instead of this . . .

var query = from s in db.Tom
select s;

it would be . . .

var query = from s in db.TableName
select s;

I have tried to google this, but I am probably phrasing the question
wrong because I don't seem to come up relevant pages.

Thanks again.
Greg

Cerebrus

unread,
Nov 24, 2009, 7:19:39 AM11/24/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
You cannot do it directly. You'd need to either reflect upon the
DataContext and lookup the property matching the table name... or...
use a dynamic sql statement that is executed using
Datacontext.ExecuteQuery().

Try googling "linq+sql+dynamic+table+name".
Reply all
Reply to author
Forward
0 new messages