实现CopyToDataTable 的T不为DataRow的查询方法(和MSDN上的方法一样)

本文探讨了在LINQtoDataSet中如何将查询结果转换为DataTable,通过具体实例展示了如何从Orders和Employees表中筛选数据,并将其注入到DataTable中,最终形成DataSet,便于与GridView等UI组件绑定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在研究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;
        }


实现

   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;
        }


你把这个 地址的类复制过来.放在一起就能实现了;

再在页面去和GridView绑定就可以拉


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值