zoukankan      html  css  js  c++  java
  • Linq&Lumda---LINQ to DataSet的DataTable操作

    1. DataTable读取列表

    DataSet ds = new DataSet();
    // 省略ds的Fill代码
    DataTable products = ds.Tables["Product"];
    IEnumerable<DataRow> rows = from p in products.AsEnumerable()
                                select p;
    foreach (DataRow row in rows)
    {
        Console.WriteLine(row.Field<string>("ProductName"));
    }

    DataSet ds = new DataSet();
    // 省略ds的Fill代码
    DataTable products = ds.Tables["Product"];
    var rows = products.AsEnumerable()
        .Select(p => new
        {
            ProductID = p.Field<int>("ProductID"),
            ProductName = p.Field<string>("ProductName"),
            UnitPrice = p.Field<decimal>("UnitPrice")
        });
    foreach (var row in rows)
    {
        Console.WriteLine(row.ProductName);
    }

    var products = ds.Tables["Product"].AsEnumerable();

    var query = from p in products select p.Field<string>("ProductName");

    2. DataTable查询

    var rows = products.AsEnumerable()

              .Where(p => p.Field<decimal>("UnitPrice") > 10m)

              .Select(p => new {

                      ProductID = p.Field<int>("ProductID"),

                      ProductName = p.Field<string>("ProductName"),

                      UnitPrice = p.Field<decimal>("UnitPrice")

                      });

    3. DataTable数据排序

    var rows = products.AsEnumerable()
        .Where(p => p.Field<decimal>("UnitPrice") > 10m)
        .OrderBy(p => p.Field<int>("SortOrder"))
        .Select(p => new
        {
            ProductID = p.Field<int>("ProductID"),
            ProductName = p.Field<string>("ProductName"),
            UnitPrice = p.Field<decimal>("UnitPrice")
        });

    var expr = from p in ds.Tables["Product"].AsEnumerable()

                orderby p.Field<int>("SortOrder"), p.Field<string>("ProductName") descending

                select p;

    4. 多个DataTable查询

    var query = from p in ds.Tables["Product"].AsEnumerable()
                from c in ds.Tables["Category"].AsEnumerable()
                where p.Field<int>("CategoryID") == c.Field<int>("CategoryID")
                    && p.Field<decimal>("UnitPrice") > 10m
                select new
                {
                    ProductID = p.Field<int>("ProductID"),
                    ProductName = p.Field<string>("ProductName"),
                    CategoryName = c.Field<string>("CategoryName")
                };

    5. DataTable分组

    var query = from p in ds.Tables["Product"].AsEnumerable()
                group p by p.Field<int>("CategoryID") into g
                select new
                {
                    CategoryID = g.Key,
                    Products = g
                };

    foreach (var item in query)
    {
        Console.WriteLine(item.CategoryID);
        foreach (var p in item.Products)
        {
            Console.WriteLine(p.Field<string>("ProductName"));
        }
    }

    var expr = from p in ds.Tables["Product"].AsEnumerable()

          group p by p.Field<int>("CategoryID") into g

          select new { CategoryID = g.Key, ProductsCount = g.Count() };

  • 相关阅读:
    万兆铜缆--七类双绞线--光纤等内容
    [51CTO]反客为主 ,Linux 成为微软 Azure 上最流行的操作系统
    [知乎]超线程对游戏来说真的没用吗?
    SQLSERVER2017 最新补丁发布方式
    MSTSC 修改端口的简单方法 3389
    使用WinSW 将 exe 创建成Windows下面 service的方法 (将nginx创建成 services)
    [时政]在美国,是参议院议长的权力大,还是众议院议长的权力大
    内网内使用https 和 使用http 建立连接的速度对比
    Windows下 OpenSSL的安装与简单使用
    [转发]VMware厚置备延迟置零 、 厚置备置零、精简置备 区别
  • 原文地址:https://www.cnblogs.com/jeffry/p/6089879.html
Copyright © 2011-2022 走看看