实现CopyToDataTable 的T不为DataRow的查询方法(和MSDN上的方法一样)
今天在研究LINQ to DataSet的分层结构中的应用和一个在泛性T不是DataRow的情况下,如何通过使用LINQ to DataSet返回DataTable,我发现为MSDN上有一个动态的将你查询结果Source的每一个枚举一个一个的注入到DataTable中的DataRow对象中..
看看怎样实现的..
数据
[Table(Name = "Orders")]
public class Orders
{
[Column(DbType = "nchar(5) null")]
public string CustomerID;
[Column(DbType = "int null")]
public int EmployeeID;
[Column(DbType = "datetime null")]
public string OrderDate;
}
[Table(Name = "Employees")]
public class Employees
{
[Column(DbType = "int not null")]
public int EmployeeID;
[Column(DbType = "nvarchar(10) not null")]
public string FirstName;
[Column(DbType = "nvarchar(30) null")]
public string Title;
}
实现
你把这个地址的类复制过来.放在一起就能实现了;
再在页面去和GridView绑定就可以拉
看看怎样实现的..
数据
[Table(Name = "Orders")]
public class Orders
{
[Column(DbType = "nchar(5) null")]
public string CustomerID;
[Column(DbType = "int null")]
public int EmployeeID;
[Column(DbType = "datetime null")]
public string OrderDate;
}
[Table(Name = "Employees")]
public class Employees
{
[Column(DbType = "int not null")]
public int EmployeeID;
[Column(DbType = "nvarchar(10) not null")]
public string FirstName;
[Column(DbType = "nvarchar(30) null")]
public string Title;
}
实现
public DataSet createDataTable()
{
DataContext context = new DataContext(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
Table<LINQtoDS_WebApp.BussinessEntity.Orders> orderTable = context.GetTable<LINQtoDS_WebApp.BussinessEntity.Orders>();
Table<LINQtoDS_WebApp.BussinessEntity.Employees> empTable = context.GetTable<LINQtoDS_WebApp.BussinessEntity.Employees>();
var query1 = from o in orderTable
where o.CustomerID.StartsWith("V")
select o;
var query2 = from es in empTable select es;
DataTable orderDataTable = new DataTable("OrdersTable");
DataTable empDataTable = new DataTable("EmployeesTable");
DataSet ds = new DataSet();
ds.Tables.Add(DataSetLinqOperators.CopyToDataTable(source, orderDataTable, LoadOption.PreserveChanges));
ds.Tables.Add(DataSetLinqOperators.CopyToDataTable(source2, empDataTable, LoadOption.PreserveChanges));
return ds;
}
{
DataContext context = new DataContext(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
Table<LINQtoDS_WebApp.BussinessEntity.Orders> orderTable = context.GetTable<LINQtoDS_WebApp.BussinessEntity.Orders>();
Table<LINQtoDS_WebApp.BussinessEntity.Employees> empTable = context.GetTable<LINQtoDS_WebApp.BussinessEntity.Employees>();
var query1 = from o in orderTable
where o.CustomerID.StartsWith("V")
select o;
var query2 = from es in empTable select es;
DataTable orderDataTable = new DataTable("OrdersTable");
DataTable empDataTable = new DataTable("EmployeesTable");
DataSet ds = new DataSet();
ds.Tables.Add(DataSetLinqOperators.CopyToDataTable(source, orderDataTable, LoadOption.PreserveChanges));
ds.Tables.Add(DataSetLinqOperators.CopyToDataTable(source2, empDataTable, LoadOption.PreserveChanges));
return ds;
}
你把这个地址的类复制过来.放在一起就能实现了;
再在页面去和GridView绑定就可以拉