• LINQ系列:LINQ to DataSet的DataTable操作


      LINQ to DataSet需要使用System.Core.dll、System.Data.dll和System.Data.DataSetExtensions.dll,在项目中添加引用System.Data和System.Data.DataSetExtensions。

    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;

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

      查询Product中每个CategoryID的数目:

    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()
               };
  • 相关阅读:
    Code First 迁移
    使用C#创建Windows服务 并发布Windows 服务
    线程(Thread,ThreadPool)、Task、Parallel
    用《捕鱼达人》去理解C#中的多线程
    Visual Studio提示“无法启动IIS Express Web服务器”的解决方法 调试闪退
    防cc攻击策略
    多线程和异步的异同和使用场景
    JQuery Checkbox 获取多选值 Checkbox选中个数
    C# 说说lock到底锁谁?(1)
    C# 说说lock到底锁谁?(2)
  • 原文地址:https://www.cnblogs.com/libingql/p/4045807.html
Copyright © 2020-2023  润新知