重视Linq技术_1

LinqPad学习与实践

推荐一个Linq学习工具:LinqPad

网址:http://www.linqpad.net/

下载:

for .net 3.5:http://www.linqpad.net/GetFile.aspx?LINQPad.exe

for .net 4.0:http://www.linqpad.net/GetFile.aspx?LINQPad4.zip

 

//1、Now for a simple LINQ-to-objects query expression (notice no semicolon):
 
from word in  "The quick brown fox jumps, over the lazy dog".Split()
orderby word.Length
select word
//2、 Setting the query language to "C# Statement(s)" permits multiple statements:
 
var words =
from word in  "The quick brown fox jumps over the lazy dog".Split()
orderby word.ToUpper()
select word;
 
var duplicates =
from word in words
group word.ToUpper() by word.ToUpper() into g
where g.Count() > 1
select  new { g.Key, Count = g.Count() };
// The Dump extension method writes out queries:
// Notice that we do need semicolons now!
words.Dump();
duplicates.Dump();
 
//3、Key Pionts 
 
from p in Products
let spanishOrders = p.OrderDetails.Where (o => o.Order.ShipCountry == "Spain")
where spanishOrders.Any()
orderby p.ProductName
select  new
{
p.ProductName,
p.Category.CategoryName,
Orders = spanishOrders.Count(),
TotalValue = spanishOrders.Sum (o => o.UnitPrice * o.Quantity)
}
//4、Chaining Query Operatorys
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
IEnumerable<string> query = names
	.Where   (n => n.Contains ("a"))
	.OrderBy (n => n.Length)
	.Select  (n => n.ToUpper());
query.Dump();
//5、natural ordering
int[] numbers  = { 10, 9, 18, 7, 6 };
numbers.Take (3)
.Dump("Take(3) returns the first three numbers in the sequence");
numbers.Skip (3)
.Dump ("Skip(3) returns all but the first three numbers in the sequence");
numbers.Reverse()
.Dump ("Reverse does exactly as it says");
Take_Skip_Reverse

 
//6、 Other Operators
 
int[] numbers = { 10, 9, 8, 7, 6 };
// Element operators:

numbers.First().Dump ("First");//10
numbers.Last().Dump ("Last");//6
numbers.ElementAt (1).Dump ("Second number");//9
numbers.OrderBy (n => n).First().Dump ("Lowest number");//6

// Aggregation operators:

numbers.Count().Dump ("Count");//5
numbers.Min().Dump ("Min");//6

// Quantifiers:

numbers.Contains (9).Dump ("Contains (9)");//true
numbers.Any().Dump ("Any");//true
numbers.Any (n => n % 2 == 1).Dump ("Has an odd numbered element");//true

// Set based operators:

int[] seq1 = { 1, 2, 3 };
int[] seq2 = { 3, 4, 5 };
seq1.Concat (seq2).Dump ("Concat");
seq1.Union (seq2).Dump ("Union");
 
//8、 With AsQueryable() added, you can see the translation to lambda var names = new[] { "Tom", "Dick", "Harry", "Mary", "Jay" }.AsQueryable();

IEnumerable<string> query =
from      n in names
where     n.Contains ("a")    // Filter elements
orderby   n.Length            // Sort elements
select    n.ToUpper();        // Translate each element (project)
query.Dump(); 

//9、Mixing Syntax
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };

( from n in names where n.Contains ( "a") select n).Count()
  .Dump ( "Names containing the letter 'a'"); //3

string first = ( from n in names orderby n select n).First()
  .Dump ( "First name, alphabetically"); //Dick

names.Where (n => n.Contains ( "a")).Count()
  .Dump ( "Original query, entirely in lambda syntax"); //3
 
names.OrderBy (n => n).First()
  .Dump ( "Second query, entirely in lambda syntax"); //Dick
 
//10、LINQ: Query Comprehension Syntax
image

转载于:https://www.cnblogs.com/sanic/archive/2012/02/28/2372647.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值