LINQ series: DataTable Operations for LINQ to Datasets

Source: Internet
Author: User

LINQ to datasets require the use of System.Core.dll, System.Data.dll and System.Data.DataSetExtensions.dll, add references System.Data and System.Data.DataSetExtensions to the project.

1. DataTable Read List

DataSet ds = new DataSet ();//Omit the DS's fill code 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 ();//Omit the DS's fill code 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            the products Select p.field<string> ("ProductName");

2. DataTable Query

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 Data Sorting

var rows = products. AsEnumerable ()    . Where (P = = p.field<decimal> ("UnitPrice") > 10m)    . (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 ()            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 ()           p.field<int> ("SortOrder"), p.field<string> ("ProductName") descending           Select P;

4. Multiple DataTable queries

var query = from P in DS. tables["Product"]. AsEnumerable () from the            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 Grouping

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

Query the number of each CategoryID in product:

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 series: DataTable Operations for LINQ to Datasets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.