zoukankan      html  css  js  c++  java
  • C# LINQ系列:LINQ to DataSet的DataTable操作 及 DataTable与Linq相互转换

      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()
               };
  • 相关阅读:
    Spring Cloud(面试必备):服务雪崩、降级与熔断
    SpringCloud 分布式事务解决方案
    2020年3月25java现场面试(有点料)
    SpringCloud Feign工作原理
    MySQL数据库引擎详解
    MySQL Hardware--CentOS 6修改CPU性能模式
    MySQL Hardware--网络测试
    MySQL Execution Plan--EXPLAIN用法
    MySQL Event--Event and EventScheduler
    MySQL Error--Error Code
  • 原文地址:https://www.cnblogs.com/hedianzhan/p/9010999.html
Copyright © 2011-2022 走看看