0

I need your help to resolve this issue.

How to pass table name dynamically in linq to sql in C#.net?

public IntPol(DataClasses1DataContext DC, decimal value,string tableName)
{
}

tableName will change dynamically.

Patrick Hofman
  • 148,824
  • 21
  • 237
  • 306
Sravanthi V
  • 51
  • 1
  • 3
  • Look for http://stackoverflow.com/questions/tagged/dynamic-linq; but I am not sure you can change the table dynamic – Pleun Dec 08 '14 at 14:44
  • You could check out this post for some options: http://stackoverflow.com/questions/14200778/dynamic-query-using-linq-to-sql – EGP Dec 08 '14 at 16:06

2 Answers2

0

I don't believe this is going to be possible. At least not using traditional Ling-To-SQL. You could use the L2S Connection object, hard code a T-SQL statement, and pass that to SQL Server. But, in this case, you aren't really using L2S anymore.

Randy Minder
  • 45,367
  • 49
  • 183
  • 332
0

If I understand the question correctly then you could do something like

public IntPol(DataClasses1DataContext DC, decimal value,string tableName)
{
  DC = new DataClasses1DataContext("yourConnString");
  var result = DC.Mapping.GetTables().SingleOrDefault(t => t.TableName.ToString() == tableName);
}

Or you could also do tableObject.GetType().Name but if type is dynamic might have to send Type tableType separately.

ihorbond
  • 1,845
  • 15
  • 23