Linq&Lumda---LINQ to DataSet的DataTable操作

Posted jeffery1010

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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() };

以上是关于Linq&Lumda---LINQ to DataSet的DataTable操作的主要内容,如果未能解决你的问题,请参考以下文章

3 layered & Linq to Sql - 业务实体

Linq XML to Object,其中 xml 节点具有嵌套元素

Linq to objects - 选择第一个对象

C# LINQ to Entities 无法识别方法“布尔”

实体框架/Linq to SQL:跳过和采取

LINQ to SQL语句(12)之Delete和使用Attach