摘要: Cross JoinThis sample shows how to efficiently join elements of two sequences based on equality between key expressions over the two.publicvoidLinq102(){string[]categories=newstring[]{"Beverages","Condiments","Vegetables","DairyProducts","Seafood"};L 阅读全文
posted @ 2011-03-13 21:07 i'm zjz 阅读(339) 评论(0) 推荐(0) 编辑
摘要: Deferred ExecutionThe following sample shows how query execution is deferred until the query is enumerated at a foreach statement.publicvoidLinq99(){//Sequenceoperatorsformfirst-classqueriesthat//arenotexecuteduntilyouenumerateoverthem.int[]numbers=newint[]{5,4,1,3,9,8,6,7,2,0};inti=0;varq=fromninnu 阅读全文
posted @ 2011-03-13 21:05 i'm zjz 阅读(257) 评论(0) 推荐(0) 编辑
摘要: CombineThis sample calculates the dot product of two integer vectors. It uses a user-created sequence operator, Combine, to calculate the dot product, passing it a lambda function to multiply two arrays, element by element, and sum the result.public static class CustomSequenceOperators{public static 阅读全文
posted @ 2011-03-13 21:04 i'm zjz 阅读(186) 评论(0) 推荐(0) 编辑
摘要: Concat - 1This sample uses Concat to create one sequence that contains each array's values, one after the other.publicvoidLinq94(){int[]numbersA={0,2,4,5,6,8,9};int[]numbersB={1,3,5,7,8};varallNumbers=numbersA.Concat(numbersB);Console.WriteLine("Allnumbersfrombotharrays:");foreach(varn 阅读全文
posted @ 2011-03-13 20:20 i'm zjz 阅读(369) 评论(0) 推荐(0) 编辑
摘要: Count - SimpleThis sample uses Count to get the number of unique factors of 300.publicvoidLinq73(){int[]factorsOf300={2,2,3,5,5};intuniqueFactors=factorsOf300.Distinct().Count();Console.WriteLine("Thereare{0}uniquefactorsof300.",uniqueFactors);}ResultThere are 3 unique factors of 300.Count 阅读全文
posted @ 2011-03-13 20:19 i'm zjz 阅读(304) 评论(0) 推荐(0) 编辑
摘要: Any - SimpleThis sample uses Any to determine if any of the words in the array contain the substring 'ei'.publicvoidLinq67(){string[]words={"believe","relief","receipt","field"};booliAfterE=words.Any(w=>w.Contains("ei"));Console.WriteLine( 阅读全文
posted @ 2011-03-13 20:18 i'm zjz 阅读(276) 评论(0) 推荐(0) 编辑
摘要: RangeThis sample uses Range to generate a sequence of numbers from 100 to 149 that is used to find which numbers in that range are odd and even.publicvoidLinq65(){varnumbers=fromninEnumerable.Range(100,50)selectnew{Number=n,OddEven=n%2==1?"odd":"even"};foreach(varninnumbers){Cons 阅读全文
posted @ 2011-03-13 20:17 i'm zjz 阅读(182) 评论(0) 推荐(0) 编辑
摘要: First - SimpleThis sample uses First to return the first matching element as a Product, instead of as a sequence containing a Product.publicvoidLinq58(){List<Product>products=GetProductList();Productproduct12=(frompinproductswherep.ProductID==12selectp).First();ObjectDumper.Write(product12);}R 阅读全文
posted @ 2011-03-13 20:15 i'm zjz 阅读(184) 评论(0) 推荐(0) 编辑
摘要: ToArrayThis sample uses ToArray to immediately evaluate a sequence into an array.publicvoidLinq54(){double[]doubles={1.7,2.3,1.9,4.1,2.9};varsortedDoubles=fromdindoublesorderbyddescendingselectd;vardoublesArray=sortedDoubles.ToArray();Console.WriteLine("Everyotherdoublefromhighesttolowest:" 阅读全文
posted @ 2011-03-13 20:13 i'm zjz 阅读(175) 评论(0) 推荐(0) 编辑
摘要: Distinct - 1This sample uses Distinct to remove duplicate elements in a sequence of factors of 300.publicvoidLinq46(){int[]factorsOf300={2,2,3,5,5};varuniqueFactors=factorsOf300.Distinct();Console.WriteLine("Primefactorsof300:");foreach(varfinuniqueFactors){Console.WriteLine(f);}}ResultPri 阅读全文
posted @ 2011-03-13 20:12 i'm zjz 阅读(387) 评论(0) 推荐(0) 编辑
摘要: GroupBy - Simple 1This sample uses group by to partition a list of numbers by their remainder when divided by 5. publicvoidLinq40() { int[]numbers={5,4,1,3,9,8,6,7,2,0}; varnumberGroups= fromninnumbers groupnbyn%5intog selectnew{Remainder=g.Key,Numbers=g}; foreach(varginnumberGroups) { Console.Write 阅读全文
posted @ 2011-03-13 20:08 i'm zjz 阅读(331) 评论(0) 推荐(0) 编辑
摘要: OrderBy - Simple 1This sample uses orderby to sort a list of words alphabetically.publicvoidLinq28(){string[]words={"cherry","apple","blueberry"};varsortedWords=fromwinwordsorderbywselectw;Console.WriteLine("Thesortedlistofwords:");foreach(varwinsortedWords){C 阅读全文
posted @ 2011-03-13 19:45 i'm zjz 阅读(300) 评论(0) 推荐(0) 编辑
摘要: Take - SimpleThis sample uses Take to get only the first 3 elements of the array.publicvoidLinq20(){int[]numbers={5,4,1,3,9,8,6,7,2,0};varfirst3Numbers=numbers.Take(3);Console.WriteLine("First3numbers:");foreach(varninfirst3Numbers){Console.WriteLine(n);}}ResultFirst 3 numbers:541Take - Ne 阅读全文
posted @ 2011-03-13 19:44 i'm zjz 阅读(318) 评论(0) 推荐(0) 编辑
摘要: Select - Simple 1This sample uses select to produce a sequence of ints one higher than those in an existing array of ints.publicvoidLinq6(){int[]numbers={5,4,1,3,9,8,6,7,2,0};varnumsPlusOne=fromninnumbersselectn+1;Console.WriteLine("Numbers+1:");foreach(variinnumsPlusOne){Console.WriteLine 阅读全文
posted @ 2011-03-13 19:43 i'm zjz 阅读(365) 评论(0) 推荐(0) 编辑
摘要: Where - Simple 1This sample uses where to find all elements of an array less than 5.publicvoidLinq1(){int[]numbers={5,4,1,3,9,8,6,7,2,0};varlowNums=fromninnumberswheren<5selectn;Console.WriteLine("Numbers<5:");foreach(varxinlowNums){Console.WriteLine(x);}}ResultNumbers < 5:41320Wh 阅读全文
posted @ 2011-03-13 19:41 i'm zjz 阅读(309) 评论(0) 推荐(0) 编辑