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.
大家好我有一个可怕的数据库,我必须使用和linq到SQL是我从中检索数据的选项。我试图通过根据用户选择引入不同的表名来重用函数,并且我无法修改DataContext查询中的TEntity或Table <>。
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.
我想用Enumerable记录填充我的ViewData。
2 个解决方案
#1
You can call the ExecuteQuery method on the DataContext instance. You will want to call the overload that takes a Type instance, outlined here:
您可以在DataContext实例上调用ExecuteQuery方法。您将需要调用带有Type实例的重载,如下所示:
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.
假设您有一个为表正确归属的类型,为该类型传递该Type实例,SQL将为您提供所需的内容。
#2
As casperOne already answered, you can use ExecuteQuery
method first overload (the one that asks for a Type parameter). Since i had a similar issue and you asked an example, here is one:
正如casperOne已经回答的那样,您可以使用ExecuteQuery方法首先重载(要求Type参数的那个)。既然我有一个类似的问题,你问了一个例子,这里有一个:
public IEnumerable<YourType> RetrieveData(string tableName, string name)
{
string sql = string.Format("Select * FROM {0} where Name = '{1}'", tableName, name);
var result = YourDataContext.ExecuteQuery(typeof(YourType), sql);
return result;
}
Pay attention to YourType
since you will have to define a type that has a constructor (it can't be abstract or interface). I'd suggest you create a custom type that has exactly the same attributes that your SQL Table. If you do that, the ExecuteQuery method will automatically 'inject' the values from your table to your custom type. Like that:
请注意YourType,因为您必须定义具有构造函数的类型(它不能是抽象或接口)。我建议您创建一个与SQL表具有完全相同属性的自定义类型。如果这样做,ExecuteQuery方法将自动将表中的值“注入”到自定义类型。像那样:
//This is a hypothetical table mapped from LINQ DBML
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.ClientData")]
public partial class ClientData : INotifyPropertyChanging, INotifyPropertyChanged
{
private int _ID;
private string _NAME;
private string _AGE;
}
//This would be your custom type that emulates your ClientData table
public class ClientDataCustomType
{
private int _ID;
private string _NAME;
private string _AGE;
}
So, on the former example, the ExecuteQuery method would be:
因此,在前一个示例中,ExecuteQuery方法将是:
var result = YourDataContext.ExecuteQuery(typeof(ClientDataCustomType), sql);
#1
You can call the ExecuteQuery method on the DataContext instance. You will want to call the overload that takes a Type instance, outlined here:
您可以在DataContext实例上调用ExecuteQuery方法。您将需要调用带有Type实例的重载,如下所示:
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.
假设您有一个为表正确归属的类型,为该类型传递该Type实例,SQL将为您提供所需的内容。
#2
As casperOne already answered, you can use ExecuteQuery
method first overload (the one that asks for a Type parameter). Since i had a similar issue and you asked an example, here is one:
正如casperOne已经回答的那样,您可以使用ExecuteQuery方法首先重载(要求Type参数的那个)。既然我有一个类似的问题,你问了一个例子,这里有一个:
public IEnumerable<YourType> RetrieveData(string tableName, string name)
{
string sql = string.Format("Select * FROM {0} where Name = '{1}'", tableName, name);
var result = YourDataContext.ExecuteQuery(typeof(YourType), sql);
return result;
}
Pay attention to YourType
since you will have to define a type that has a constructor (it can't be abstract or interface). I'd suggest you create a custom type that has exactly the same attributes that your SQL Table. If you do that, the ExecuteQuery method will automatically 'inject' the values from your table to your custom type. Like that:
请注意YourType,因为您必须定义具有构造函数的类型(它不能是抽象或接口)。我建议您创建一个与SQL表具有完全相同属性的自定义类型。如果这样做,ExecuteQuery方法将自动将表中的值“注入”到自定义类型。像那样:
//This is a hypothetical table mapped from LINQ DBML
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.ClientData")]
public partial class ClientData : INotifyPropertyChanging, INotifyPropertyChanged
{
private int _ID;
private string _NAME;
private string _AGE;
}
//This would be your custom type that emulates your ClientData table
public class ClientDataCustomType
{
private int _ID;
private string _NAME;
private string _AGE;
}
So, on the former example, the ExecuteQuery method would be:
因此,在前一个示例中,ExecuteQuery方法将是:
var result = YourDataContext.ExecuteQuery(typeof(ClientDataCustomType), sql);