这里我们分为五个部分:
(1)首先先熟悉LINQ:
int
[] integers
=
{ 1, 6, 2, 27, 10, 33, 12, 8, 14, 5 }
;
IEnumerable < int > twoDigits =
from numbers in integers
where numbers >= 10
select numbers;
Console.WriteLine( " Integers > 10: " );
foreach (var number in twoDigits)
{
Console.WriteLine(number);
}
IEnumerable < int > twoDigits =
from numbers in integers
where numbers >= 10
select numbers;
Console.WriteLine( " Integers > 10: " );
foreach (var number in twoDigits)
{
Console.WriteLine(number);
}
(2)简单的查询
//
Usage generally LINQ Query
DataTable products = ds.Tables[ " Products " ];
Console.WriteLine( " Mapping Name of Table{0} " , ds.Tables[ " Products " ].TableName.ToString());
IEnumerable < DataRow > query = from product in products.AsEnumerable() select product;
Console.WriteLine( " Product Names " );
foreach (DataRow p in query)
{
Console.WriteLine(p.Field<string>("ProductName"));
}
Console.ReadLine();
DataTable products = ds.Tables[ " Products " ];
Console.WriteLine( " Mapping Name of Table{0} " , ds.Tables[ " Products " ].TableName.ToString());
IEnumerable < DataRow > query = from product in products.AsEnumerable() select product;
Console.WriteLine( " Product Names " );
foreach (DataRow p in query)
{
Console.WriteLine(p.Field<string>("ProductName"));
}
Console.ReadLine();
(3)基于Lambda Expressions表达式的查询
//
Usage Method-Based Query Syntax
// Use DataSet ds = new DataSet();
var queryInfo = products.AsEnumerable().Select(product => new
{
ProductName = product.Field<string>("ProductName"),
ProductNumber = product.Field<int>("ProductID")
} );
Console.WriteLine( " Product Info: " );
foreach (var productInfo in queryInfo)
{
Console.WriteLine("Product name: {0} Product number: {1} ",
productInfo.ProductName, productInfo.ProductNumber);
}
Console.ReadLine();
// Use DataSet ds = new DataSet();
var queryInfo = products.AsEnumerable().Select(product => new
{
ProductName = product.Field<string>("ProductName"),
ProductNumber = product.Field<int>("ProductID")
} );
Console.WriteLine( " Product Info: " );
foreach (var productInfo in queryInfo)
{
Console.WriteLine("Product name: {0} Product number: {1} ",
productInfo.ProductName, productInfo.ProductNumber);
}
Console.ReadLine();
(4)组合式查询
//
Usage Composing Queries
// Use DataSet ds = new DataSet();
IEnumerable < DataRow > productsQuery = from product in products.AsEnumerable() select product;
IEnumerable < DataRow > largeProducts = productsQuery.Where(p => p.Field < string > ( " ProductName " ) == " Chai " );
Console.WriteLine( " ProductName =='Chai' " );
foreach (DataRow product in largeProducts)
{
Console.WriteLine(product.Field<int>("ProductID"));
}
Console.ReadLine();
// Use DataSet ds = new DataSet();
IEnumerable < DataRow > productsQuery = from product in products.AsEnumerable() select product;
IEnumerable < DataRow > largeProducts = productsQuery.Where(p => p.Field < string > ( " ProductName " ) == " Chai " );
Console.WriteLine( " ProductName =='Chai' " );
foreach (DataRow product in largeProducts)
{
Console.WriteLine(product.Field<int>("ProductID"));
}
Console.ReadLine();
(5)最后就是使用ToList(TSource), ToArray(TSource),ToDictionary(TSource, TKey)
ToList(TSource), ToArray(TSource)就是分别从IEnumerable(T)中创建一个List,Array,因为我们使用LINQ插叙回来的数据都是基于IEnumerable(T)泛性接口,返回值的类型为T的指定类型.
而ToDictionary(TSource, TKey)是从IEnumerable(T)中创建一个字典.
//
toList(TSource),ToArray(TSource),ToLookup,
// Use DataSet ds = new DataSet();but we use newly Table as name is Suppliers
DataTable Suppliers = ds.Tables[ " Suppliers " ];
IEnumerable < DataRow > SuppliersQuery = from Supplier in Suppliers.AsEnumerable()
orderby
Supplier.Field < string > ( " City " ) descending
select Supplier;
IEnumerable < DataRow > SuppliersArray = SuppliersQuery.ToArray();
IEnumerable < DataRow > SupplierstoList = SuppliersQuery.ToList();
foreach (DataRow SuppArray in SuppliersArray)
{
Console.WriteLine(SuppArray.Field<String>("City"));
}
foreach (DataRow supplist in SupplierstoList)
{
Console.WriteLine(supplist.Field<String>("City"));
}
// Use DataSet ds = new DataSet();but we use newly Table as name is Suppliers
DataTable Suppliers = ds.Tables[ " Suppliers " ];
IEnumerable < DataRow > SuppliersQuery = from Supplier in Suppliers.AsEnumerable()
orderby
Supplier.Field < string > ( " City " ) descending
select Supplier;
IEnumerable < DataRow > SuppliersArray = SuppliersQuery.ToArray();
IEnumerable < DataRow > SupplierstoList = SuppliersQuery.ToList();
foreach (DataRow SuppArray in SuppliersArray)
{
Console.WriteLine(SuppArray.Field<String>("City"));
}
foreach (DataRow supplist in SupplierstoList)
{
Console.WriteLine(supplist.Field<String>("City"));
}