C# LINQ series: DataTable operation of LINQ to DataSet and conversion between DataTable and Linq

  LINQ to DataSet needs to use System.Core.dll, System.Data.dll and System.Data.DataSetExtensions.dll, add references to System.Data and System.Data.DataSetExtensions in the project.

1. DataTable read list

copy code
DataSet ds = new DataSet();
// Omit the Fill code of ds
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"));
}
copy code
copy code
DataSet ds = new DataSet();
// Omit the Fill code of ds
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);
}
copy code
var products = ds.Tables["Product"].AsEnumerable();
var query = from p in products
            select p.Field<string>("ProductName");

2. DataTable query

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

3. DataTable data sorting

copy code
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")
    });
copy code
copy code
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"));
}
copy code
var expr = from p in ds.Tables["Product"].AsEnumerable()
           orderby p.Field<int>("SortOrder"), p.Field<string>("ProductName") descending
           select p;

4. Multiple DataTable queries

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

5. DataTable grouping

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

  Query the number of each CategoryID in Product:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325917880&siteId=291194637