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

  • 相关阅读:
    数据驱动ddt简单使用
    html-testRunner在unittest测试套件中的使用
    Python设计模式----3.单例模式
    Python设计模式----2.工厂模式
    Python设计模式----1.简单工厂模式
    在小程序中对图片进行缩放时发生的问题记录
    将项目发布到Maven中央仓库的不完整纪要
    虚机的SQL Server空间占满之后进行释放的一些操作
    jdk8环境下,添加重复注解的美好体验
    使用transient关键字解决ehcache序列化错误
  • 原文地址:https://www.cnblogs.com/jeffry/p/6089879.html
Copyright © 2020-2023  润新知