zoukankan      html  css  js  c++  java
  • linq查询DataView

    转至:  http://blog.csdn.net/livening/article/details/6043081

      

      namespace UseDataView  
        {  
            class Program  
            {  
                static void Main(string[] args)  
                {  
                    CreateDataVeiw( );  
                    UseDataView( );  
                }  
          
                static DataSet BuildDataSet( )  
                {  
                    //创建Students数据集  
                    DataSet ds = new DataSet("Students");  
                    //创建Students数据表,并添加到数据集  
                    //Students数据表包含学生信息  
                    DataTable dtStu = new DataTable("Students");  
                    ds.Tables.Add(dtStu);  
                    //添加学生信息记录的列信息  
                    dtStu.Columns.AddRange(new DataColumn[]{  
                        new DataColumn("Name", Type.GetType("System.String")),  
                        new DataColumn("XingBie", Type.GetType("System.String")),  
                        new DataColumn("Age", Type.GetType("System.Int32")),  
                    });  
                    //添加学生信息的行信息  
                    dtStu.Rows.Add("张三", "男", 20);  
                    dtStu.Rows.Add("李四", "男", 19);  
                    dtStu.Rows.Add("王霞", "女", 21);  
                    dtStu.Rows.Add("赵敏", "女", 22);  
                    dtStu.Rows.Add("吴安", "男", 18);  
                    dtStu.Rows.Add("杨花", "女", 23);  
                    //返回数据集  
                    return ds;  
                }  
          
                static void CreateDataVeiw( )  
                {  
                    //获取数据源  
                    DataSet ds = BuildDataSet( );  
                    DataTable dt = ds.Tables["Students"];  
                    //用DataTable创建DataView  
                    DataView dvDt = dt.AsDataView( );  
                    //用LINQ查询创建DataView  
                    EnumerableRowCollection<DataRow> query1 =  
                        from stu in dt.AsEnumerable( )  
                        select stu;  
                    DataView dvNml = query1.AsDataView( );  
                    //用LINQ查询创建具有过滤信息的DataView  
                    EnumerableRowCollection<DataRow> query2 =  
                        from stu in dt.AsEnumerable( )  
                        where stu.Field<string>("Name").StartsWith("杨")  
                        select stu;  
                    DataView dvFilter = query2.AsDataView( );  
                    //用LINQ查询创建具有排序信息的DataView  
                    EnumerableRowCollection<DataRow> query3 =  
                        from stu in dt.AsEnumerable( )  
                        orderby stu.Field<int>("Age")  
                        select stu;  
                    DataView dvSort = query3.AsDataView( );  
                }  
          
                static void UseDataView( )  
                {  
                    //获取数据源  
                    DataSet ds = BuildDataSet( );  
                    DataTable dt = ds.Tables["Students"];  
                    //用DataTable创建DataView  
                    DataView dvDt = dt.AsDataView( );  
                    //通过RowFilter属性设置DataView过滤信息,只需要年龄大于20岁的学生记录  
                    dvDt.RowFilter = "Age > 20";  
                    //设置RowFilter为null或空字符串,清除过滤信息,二选一  
                    dvDt.RowFilter = string.Empty;  
                    dvDt.RowFilter = null;  
                    //通过Sort属性设置DataView过滤信息  
                    dvDt.Sort = "Age asc, Name desc";  
                    //设置RowFilter为null或空字符串,清除过滤信息,二选一  
                    dvDt.Sort = string.Empty;  
                    dvDt.Sort = null;  
                }  
            }  
        } 

  • 相关阅读:
    算法
    UVA 10318 Security Panel(DFS剪枝 + 状压 + 思维)题解
    CodeForces 509C Sums of Digits(贪心乱搞)题解
    UVA 10382 Watering Grass(区间覆盖,贪心)题解
    CodeForces 430A Points and Segments (easy)(构造)题解
    CodeForces 459C Pashmak and Buses(构造)题解
    newcoder F石头剪刀布(DFS + 思维)题解
    newcoder H肥猪(单调队列 / 线段树)题解
    UVALive 7501 Business Cycle(二分)题解
    UVALive 7503 Change(乱搞)题解
  • 原文地址:https://www.cnblogs.com/shi5588/p/2934575.html
Copyright © 2011-2022 走看看