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
  • 153,850
  • 22
  • 249
  • 325
Sravanthi V
  • 51
  • 1
  • 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
  • 47,200
  • 49
  • 204
  • 358
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.

ihor.eth
  • 2,207
  • 20
  • 27