Linq to SQL - Dynamic Table Name

I am using Linq to SQL, including Dynamic Linq queries. The example from documentation gives examples of retrieving dynamic columns using dynamic conditions, but not from a dynamic table itself. I do not know the table name my user wants until run time.

If I add "DataContext1.MyTable" to the watch, it shows "{Table(MyTable)}"

I get the same from the below using Reflections...
object table = DataContext1.GetType().GetProperty("MyTable").GetValue(DataContext1, null);

Which is good... but I am unable to use it for further querying.


Please help with the above or suggest your own solution for retrieving rows from a table mentioned during run time. I know both the table name and the list of all columns during run time.


I know I can simply resort to using SQL, but want to know if there is a Linq based solution.
LVL 9
Shahid ThaikaSole ProprietorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tovvenkiCommented:
0
_Katka_Commented:
Hi, can't you just cast that table (object) to yours MyTable.

MyTable table = (MyTable)DataContext1.GetType().GetProperty("MyTable").GetValue(DataContext1, null);

regards,
Kate
0
Shahid ThaikaSole ProprietorAuthor Commented:
Wow!! I actually missed one of the four links that were posted during my own google search, and that had to be the one with the solution!!! Need to up my google skills a bit.

This was the solution that worked for me...

Type someEntity = Type.GetType("Namespace.EntityClassName");
IQueryable q = dc.GetTable(someEntity);

foreach (var someVariable in q)
{
}

...from http://social.msdn.microsoft.com/Forums/en/linqprojectgeneral/thread/af89b44f-8c8d-45b8-94c0-a3ff69528ccf

So my code now looks like the one below...

Problem is how do I get the individual values out of the variable in the foreach loop? If I do something like "row. ", then it only gives four options... including .GetType(), .ToString(). If I add to the watch, I can see it is an object of my Table's Class type with all the columns and data, but I can't cast it, since I don't know the table's name until runtime.


NOTE: I have already found a solution for that as well, but I am keeping this open to see if any of you can come up with a better one.
string strTableName = "MyTable";
string columnName = "Field1";
Type tType = Type.GetType("MyProject." + strTableName);
IQueryable q = dc.GetTable(tType);

foreach (var someVariable in q)
{
    //How to get the column data out of the row?
    //Column names are known during runtime
    
}

Open in new window

0
Shahid ThaikaSole ProprietorAuthor Commented:
Part of me closing my old topics. A link lead me to some direction, but not the solution I was looking for. I ended up using a different approach for solving my task
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.