由于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;
}
注重YourType
,因为你必须定义有一个构造函数(它不能是抽象或接口)类型。我建议你创建一个自定义类型,它具有与你的SQL Table完全相同的属性。如果这样做,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;
}
所以,关于前者的例子,executeQuery方法是:
var result = YourDataContext.ExecuteQuery(typeof(ClientDataCustomType), sql);
你介意提供了一个例子。 – Ayo 2009-02-17 21:25:27