views:

1099

answers:

3

I need to get table data from table name from Linq DataContext.

Instead of this

var results = db.Authors;

I need to do something like this.

string tableName = "Authors";

var results = db[tableName];

It could be any table name that is available in DataContext.

+2  A: 

I am not sure if passing strings is an elegant solution. I would rather send the Type of entity as an argument to a method. Something on these lines :

var table = _dataCont.GetTable(typeof(Customer));

Here is the MSDN documentation.

Perpetualcoder
I need to use my string variable to get the table data.
Krunal
since by design Linq2SQl does not support that. You will need to make a method that looks like Type GetTableTypeFromString(string tableName); Have a switch statement and return desired table type
Perpetualcoder
+3  A: 

I am not sure I'd suggest it as a GOOD solution, but if you really need it, you could do something like this:

MyDBContext db = new MyDBContext();
Type t = db.GetType();
PropertyInfo p = t.GetProperty("Authors");
var table = p.GetValue(db, null);

That will give you the Authors table, as pr. Table.

Terje
This will type `table` as an `object`; you need to cast to an `ITable` before use.
Jason
Can you provide how to Use ITable to get data as IEnumerable or IQueryable?
Krunal
It is already both an IEnumerable/IQueryable. The difference is it is not the generics version IEnumerable<T>/IQueryable<T>, so you can't use strongly typed queries on it. But you can use Dynamic linq to perform weakly typed queries. More information here: http://www.beansoftware.com/ASP.NET-Tutorials/Dynamic-LINQ.aspx and download it here: http://code.msdn.microsoft.com/csharpsamples/Release/ProjectReleases.aspx?ReleaseId=8 (You're looking for dynamic.cs)
Terje
ITable tbl = db.GetTableByName("Authors");var data = (from m in tbl select m).ToList();I tried this way... but getting compile error : Error 1 Could not find an implementation of the query pattern for source type 'System.Data.Linq.ITable'. 'Select' not found. Consider explicitly specifying the type of the range variable 'm'.
Krunal
Hi. You can't use it this way. Like I said, you cannot use strongly typed queries here, so you would have to do a foreach(var tblrow in tbl) {} to iterate and perform if's on each element. Or, you can maybe look at Dynamic Linq.
Terje
+2  A: 

Here's an extension method that will do what you want.

static class DataContextExtensions {
    public static ITable GetTableByName(this DataContext context, string tableName) {
        if (context == null) {
            throw new ArgumentNullException("context");
        }
        if (tableName == null) {
            throw new ArgumentNullException("tableName");
        }
        return (ITable)context.GetType().GetProperty(tableName).GetValue(context, null);
    }
}

Sample:

TextContext db = new TestContext();
db.GetTableByName("MyObjects");
Jason
Ok, ITable tbl = db.GetTableByName("Authors"); how to access tbl to get the IEnumerable records?
Krunal
An `ITable` is also an `IEnumerable`, so enumerate the records using your favorite method (e.g., `foreach(object r in tbl) { }`).
Jason
But it is not strongly typed. how can i cast dynamically it to get strongly typed data?
Krunal
That's impossible. If you want to look up the table name dynamically then you lose the ability to have a strongly-typed table.
Jason