Get Table-Data from Table-Name in Linq Datacontext

Get table-data from table-name in LINQ DataContext

Given DataContext context and string tableName, you can just say:

var table = (ITable)context.GetType()
.GetProperty(tableName)
.GetValue(context, null);

Getting name of specific linq to sql table

I am not sure if this works for EF, but I use this approach in Linq to Sql.

You'll have to use attributes from System.Data.Linq.Mapping namespace. If you open the *.designer.cs file, containing the definition of any Linq to Sql entity, you'll find a line like this above the declaration of the class:

[global::System.Data.Linq.Mapping.TableAttribute(Name="YourTableName")]

So each entity class in Linq to Sql is marked with the TableAttribute attribute and it's Name property contains the name you need. We may use this:

public static string GetTableName<TEntity>(this Table<TEntity> MyTable) 
where TEntity : class
{
Type type = typeof(TEntity);
object[] temp = type.GetCustomAttributes(
typeof(System.Data.Linq.Mapping.TableAttribute),
true);
if (temp.Length == 0)
return null;
else
return (temp[0] as System.Data.Linq.Mapping.TableAttribute).Name;
}

Dynamic Table Names in Linq to SQL

You can call the ExecuteQuery method on the DataContext instance. You will want to call the overload that takes a Type instance, outlined here:

http://msdn.microsoft.com/en-us/library/bb534292.aspx

Assuming that you have a type that is attributed correctly for the table, passing that Type instance for that type and the SQL will give you what you want.

LINQ Select from dynamic tableName string

The var query = table.Where(....).Select(...) is the correct move as it allows reflection for the query builder at runtime. However, t.AccountID is an error because of the type of t remains unknown.

I've previously used a similar approach in LINQ to SQL, using System.Linq.Expressions.Expression, e.g.:

    // NOT TESTED
var table=context.GetTable(dynamicTableName);
var theT=table.Experssion; // actually, I forget. DynamicExpression or MemberBinding? or
var theField=Expression.Field(theT, "AccountID"); // or dynamic name
var query=table.Where(Expression.Equal(theField, accID);
var recList=query.ToList<object>();

If your object has a common interface there is a simpler syntax:

IQueryable<MyInterface> table = context.GetTable("table") as IQueryable<MyInterface>;
var recList=from r in table
where table.AccountID == ac // if your AccountID is on MyInterface
select table;

If you only have a few tables to support, you could do this as well:

    IQueryable<MyInterface> table;
if("table1"==tableName)
table=_db.table1
elseif("table2"==tableName)
table=_db.table2
elseif("table3"==tableName)
table=_db.table3
else
throw exception

Linq: Get a list of all tables within DataContext

It's much easier than above and no reflection required. Linq to SQL has a Mapping property that you can use to get an enumeration of all the tables.

context.Mapping.GetTables();

How to get table from sql with linq?

The problem is in what you are passing to the constructor of DataContext. It should be a proper connection string to your local server. The mdf file must be attached to it.

  [Table(Name = "MyTable")]
class MyTable
{
[Column] public int Id { get; set; }
[Column] public string SomethingElse { get; set; }
}
class Program
{
static void Main(string[] args)
{
DataContext db = new DataContext(@"Data Source=(localdb)\v11.0;
Integrated Security=true;
AttachDbFileName=G:\Progress\TestingTools\ReadingThruDataContext\ReadingThruDataContext\DailyDemand2.mdf");
var firstResult = db.GetTable<MyTable>().FirstOrDefault();
var allItems = db.GetTable<MyTable>().ToList();

}
}


Related Topics



Leave a reply



Submit