views:

1263

answers:

1

Hi all I have a horrid database I gotta work with and linq to sql is the option im taking to retrieve data from. anywho im trying to reuse a function by throwing in a different table name based on a user selection and there is no way to my knowledge to modify the TEntity or Table<> in a DataContext Query.

This is my current code.

public void GetRecordsByTableName(string table_name){

string sql = "Select * from " + table_name;
var records = dataContext.ExecuteQuery</*Suppossed Table Name*/>(sql);

ViewData["recordsByTableName"] = records.ToList();
}

I want to populate my ViewData with Enumerable records.

+1  A: 

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.

casperOne
Do you mind providing an example.
Ayo
Nice, I never knew that.
jeremcc