• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
works guo

成功的渴望与生俱来---- 成功是获得赞美与尊重的最有效的途径
博客园    首页    新随笔    联系   管理    订阅  订阅

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

LINQ to DataSet中实现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;
        }


实现

  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绑定就可以拉


posted @ 2008-04-16 23:55  works guo  阅读(1986)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3