zoukankan      html  css  js  c++  java
  • 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 products.AsEnumerable()
    orderby p.Field<int>("SortOrder")
    select p;
    IEnumerable<DataRow> rows = expr.ToArray();
    foreach (var row in rows)
    {
    Console.WriteLine(row.Field<string>("ProductName"));
    }

    var expr = from p in ds.Tables["Product"].AsEnumerable()
               orderby p.Field<int>("SortOrder"), p.Field<string>("ProductName") descending
               select p;


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

















  • 相关阅读:
    自学Linux Shell14.3-创建临时文件
    自学Linux Shell14.2-在脚本中使用其他文件描述符
    自学Linux Shell14.1-理解输入输出
    自学Linux Shell13.3-获得用户输入(read命令)
    自学Linux Shell13.2-选项处理(主要getopt、getopts命令)
    自学Linux Shell13.1-命令行参数
    自学Linux Shell12.8-循环实例
    自学Linux Shell12.7-控制循环break、continue命令
    自学Linux Shell12.6-嵌套循环for命令
    自学Linux Shell12.5-while、until命令
  • 原文地址:https://www.cnblogs.com/haofaner/p/5166622.html
Copyright © 2011-2022 走看看