101个LINQ示例,包含几乎全部操作
2016年4月22日ASP.NET Standard
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 | Restriction Operators Where - Simple 1 public void Linq1() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var lowNums = from n in numbers where n < 5 select n; Console.WriteLine("Numbers < 5:"); foreach (var x in lowNums) { Console.WriteLine(x); } } Where - Simple 2 public void Linq2() { List products = GetProductList(); var soldOutProducts = from p in products where p.UnitsInStock == 0 select p; Console.WriteLine("Sold out products:"); foreach (var product in soldOutProducts) { Console.WriteLine("{0} is sold out!", product.ProductName); } } Where - Simple 3 public void Linq3() { List products = GetProductList(); var expensiveInStockProducts = from p in products where p.UnitsInStock > 0 && p.UnitPrice > 3.00M select p; Console.WriteLine("In-stock products that cost more than 3.00:"); foreach (var product in expensiveInStockProducts) { Console.WriteLine("{0} is in stock and costs more than 3.00.", product.ProductName); } } Where - Drilldown public void Linq4() { List customers = GetCustomerList(); var waCustomers = from c in customers where c.Region == "WA" select c; Console.WriteLine("Customers from Washington and their orders:"); foreach (var customer in waCustomers) { Console.WriteLine("Customer {0}: {1}", customer.CustomerID, customer.CompanyName); foreach (var order in customer.Orders) { Console.WriteLine(" Order {0}: {1}", order.OrderID, order.OrderDate); } } } Where - Indexed public void Linq5() { string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var shortDigits = digits.Where((digit, index) => digit.Length < index); Console.WriteLine("Short digits:"); foreach (var d in shortDigits) { Console.WriteLine("The word {0} is shorter than its value.", d); } } Projection Operators Select - Simple 1 public void Linq6() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var numsPlusOne = from n in numbers select n + 1; Console.WriteLine("Numbers + 1:"); foreach (var i in numsPlusOne) { Console.WriteLine(i); } } Select - Simple 2 public void Linq7() { List products = GetProductList(); var productNames = from p in products select p.ProductName; Console.WriteLine("Product Names:"); foreach (var productName in productNames) { Console.WriteLine(productName); } } Select - Transformation public void Linq8() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var textNums = from n in numbers select strings[n]; Console.WriteLine("Number strings:"); foreach (var s in textNums) { Console.WriteLine(s); } } Select - Anonymous Types 1 public void Linq9() { string[] words = { "aPPLE", "BlUeBeRrY", "cHeRry" }; var upperLowerWords = from w in words select new {Upper = w.ToUpper(), Lower = w.ToLower()}; foreach (var ul in upperLowerWords) { Console.WriteLine("Uppercase: {0}, Lowercase: {1}", ul.Upper, ul.Lower); } } Select - Anonymous Types 2 public void Linq10() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var digitOddEvens = from n in numbers select new {Digit = strings[n], Even = (n % 2 == 0)}; foreach (var d in digitOddEvens) { Console.WriteLine("The digit {0} is {1}.", d.Digit, d.Even ? "even" : "odd"); } } Select - Anonymous Types 3 public void Linq11() { List products = GetProductList(); var productInfos = from p in products select new {p.ProductName, p.Category, Price = p.UnitPrice}; Console.WriteLine("Product Info:"); foreach (var productInfo in productInfos) { Console.WriteLine("{0} is in the category {1} and costs {2} per unit.", productInfo.ProductName, productInfo.Category, productInfo.Price); } } Select - Indexed public void Linq12() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var numsInPlace = numbers.Select((num, index) => new {Num = num, InPlace = (num == index)}); Console.WriteLine("Number: In-place?"); foreach (var n in numsInPlace) { Console.WriteLine("{0}: {1}", n.Num, n.InPlace); } } Select - Filtered public void Linq13() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var lowNums = from n in numbers where n < 5 select digits[n]; Console.WriteLine("Numbers < 5:"); foreach (var num in lowNums) { Console.WriteLine(num); } } SelectMany - Compound from 1 public void Linq14() { int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; int[] numbersB = { 1, 3, 5, 7, 8 }; var pairs = from a in numbersA, b in numbersB where a < b select new {a, b}; Console.WriteLine("Pairs where a < b:"); foreach (var pair in pairs) { Console.WriteLine("{0} is less than {1}", pair.a, pair.b); } } SelectMany - Compound from 2 public void Linq15() { List customers = GetCustomerList(); var orders = from c in customers, o in c.Orders where o.Total < 500.00M select new {c.CustomerID, o.OrderID, o.Total}; ObjectDumper.Write(orders); } SelectMany - Compound from 3 public void Linq16() { List customers = GetCustomerList(); var orders = from c in customers, o in c.Orders where o.OrderDate >= new DateTime(1998, 1, 1) select new {c.CustomerID, o.OrderID, o.OrderDate}; ObjectDumper.Write(orders); } SelectMany - from Assignment public void Linq17() { List customers = GetCustomerList(); var orders = from c in customers, o in c.Orders, total = o.Total where total >= 2000.0M select new {c.CustomerID, o.OrderID, total}; ObjectDumper.Write(orders); } SelectMany - Multiple from public void Linq18() { List customers = GetCustomerList(); DateTime cutoffDate = new DateTime(1997, 1, 1); var orders = from c in customers where c.Region == "WA" from o in c.Orders where o.OrderDate >= cutoffDate select new {c.CustomerID, o.OrderID}; ObjectDumper.Write(orders); } SelectMany - Indexed public void Linq19() { List customers = GetCustomerList(); var customerOrders = customers.SelectMany( (cust, custIndex) => cust.Orders.Select(o => "Customer #" + (custIndex + 1) + " has an order with OrderID " + o.OrderID) ); ObjectDumper.Write(customerOrders); } Partitioning Operators Take - Simple public void Linq20() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var first3Numbers = numbers.Take(3); Console.WriteLine("First 3 numbers:"); foreach (var n in first3Numbers) { Console.WriteLine(n); } } Take - Nested public void Linq21() { List<Customer> customers = GetCustomerList(); var first3WAOrders = ( from c in customers from o in c.Orders where c.Region == "WA" select new {c.CustomerID, o.OrderID, o.OrderDate} ) .Take(3); Console.WriteLine("First 3 orders in WA:"); foreach (var order in first3WAOrders) { ObjectDumper.Write(order); } } Skip - Simple public void Linq22() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var allButFirst4Numbers = numbers.Skip(4); Console.WriteLine("All but first 4 numbers:"); foreach (var n in allButFirst4Numbers) { Console.WriteLine(n); } } Skip - Nested public void Linq23() { List<Customer> customers = GetCustomerList(); var waOrders = from c in customers from o in c.Orders where c.Region == "WA" select new {c.CustomerID, o.OrderID, o.OrderDate}; var allButFirst2Orders = waOrders.Skip(2); Console.WriteLine("All but first 2 orders in WA:"); foreach (var order in allButFirst2Orders) { ObjectDumper.Write(order); } } TakeWhile - Simple public void Linq24() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 6); Console.WriteLine("First numbers less than 6:"); foreach (var n in firstNumbersLessThan6) { Console.WriteLine(n); } } SkipWhile - Simple public void Linq26() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var allButFirst3Numbers = numbers.SkipWhile(n => n % 3 != 0); Console.WriteLine("All elements starting from first element divisible by 3:"); foreach (var n in allButFirst3Numbers) { Console.WriteLine(n); } } SkipWhile - Indexed public void Linq27() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var laterNumbers = numbers.SkipWhile((n, index) => n >= index); Console.WriteLine("All elements starting from first element less than its position:"); foreach (var n in laterNumbers) { Console.WriteLine(n); } } Ordering Operators OrderBy - Simple 1 publicvoid Linq28() { string[] words = { "cherry", "apple", "blueberry" }; var sortedWords = from w in words orderby w select w; Console.WriteLine("The sorted list of words:"); foreach (var w in sortedWords) { Console.WriteLine(w); } } OrderBy - Simple 2 public void Linq29() { string[] words = { "cherry", "apple", "blueberry" }; var sortedWords = from w in words orderby w.Length select w; Console.WriteLine("The sorted list of words (by length):"); foreach (var w in sortedWords) { Console.WriteLine(w); } } OrderBy - Simple 3 public void Linq30() { List products = GetProductList(); var sortedProducts = from p in products orderby p.ProductName select p; ObjectDumper.Write(sortedProducts); } OrderBy - Comparer public class CaseInsensitiveComparer : IComparer<string> { public int Compare(string x, string y) { return string.Compare(x, y, true); } } public void Linq31() { string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry"}; var sortedWords = words.OrderBy(a => a, new CaseInsensitiveComparer()); ObjectDumper.Write(sortedWords); } OrderByDescending - Simple 1 public void Linq32() { double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 }; var sortedDoubles = from d in doubles orderby d descending select d; Console.WriteLine("The doubles from highest to lowest:"); foreach (var d in sortedDoubles) { Console.WriteLine(d); } } OrderByDescending - Simple 2 public void Linq33() { List products = GetProductList(); var sortedProducts = from p in products orderby p.UnitsInStock descending select p; ObjectDumper.Write(sortedProducts); } OrderByDescending - Comparer public class CaseInsensitiveComparer : IComparerspan class="qs-keyword">string> { publicint Compare(string x, string y) { returnstring.Compare(x, y, true); } } publicvoid Linq34() { string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry"}; var sortedWords = words.OrderByDescending(a => a, new CaseInsensitiveComparer()); ObjectDumper.Write(sortedWords); } ThenBy - Simple publicvoid Linq35() { string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var sortedDigits = from d in digits orderby d.Length, d select d; Console.WriteLine("Sorted digits:"); foreach (var d in sortedDigits) { Console.WriteLine(d); } } ThenBy - Comparer public class CaseInsensitiveComparer : IComparerspan class="qs-keyword">string> { publicint Compare(string x, string y) { returnstring.Compare(x, y, true); } } publicvoid Linq36() { string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry"}; var sortedWords = words.OrderBy(a => a.Length) .ThenBy(a => a, new CaseInsensitiveComparer()); ObjectDumper.Write(sortedWords); } ThenByDescending - Simple publicvoid Linq37() { List products = GetProductList();var sortedProducts = from p in products orderby p.Category, p.UnitPrice descendingselect p; ObjectDumper.Write(sortedProducts); } ThenByDescending - Comparer public class CaseInsensitiveComparer : IComparerspan class="qs-keyword">string> { publicint Compare(string x, string y) { returnstring.Compare(x, y, true); } } publicvoid Linq38() { string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry"}; var sortedWords = words.OrderBy(a => a.Length) .ThenByDescending(a => a, new CaseInsensitiveComparer()); ObjectDumper.Write(sortedWords); } Reverse publicvoid Linq39() { string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var reversedIDigits = ( from d in digits where d[1] == 'i' select d) .Reverse(); Console.WriteLine("A backwards list of the digits with a second character of 'i':"); foreach (var d in reversedIDigits) { Console.WriteLine(d); } } Grouping Operators GroupBy - Simple 1 public void Linq40() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var numberGroups = from n in numbers group n by n % 5 into g select new { Remainder = g.Key, Numbers = g }; foreach (var g in numberGroups) { Console.WriteLine("Numbers with a remainder of {0} when divided by 5:", g.Remainder); foreach (var n in g.Numbers) { Console.WriteLine(n); } } } GroupBy - Simple 2 public void Linq41() { string[] words = { "blueberry", "chimpanzee", "abacus", "banana", "apple", "cheese" }; var wordGroups = from w in words group w by w[0] into g select new { FirstLetter = g.Key, Words = g }; foreach (var g in wordGroups) { Console.WriteLine("Words that start with the letter '{0}':", g.FirstLetter); foreach (var w in g.Words) { Console.WriteLine(w); } } } GroupBy - Simple 3 public void Linq42() { List<Product> products = GetProductList(); var orderGroups = from p in products group p by p.Category into g select new { Category = g.Key, Products = g }; ObjectDumper.Write(orderGroups, 1); } GroupBy - Nested public void Linq43() { List<Customer> customers = GetCustomerList(); var customerOrderGroups = from c in customers select new {c.CompanyName, YearGroups = from o in c.Orders group o by o.OrderDate.Year into yg select new {Year = yg.Key, MonthGroups = from o in yg group o by o.OrderDate.Month into mg select new { Month = mg.Key, Orders = mg } } }; ObjectDumper.Write(customerOrderGroups, 3); } GroupBy - Comparer public class AnagramEqualityComparer : IEqualityComparer { public bool Equals(string x, string y) { return getCanonicalString(x) == getCanonicalString(y); } public int GetHashCode(string obj) { return getCanonicalString(obj).GetHashCode(); } private string getCanonicalString(string word) { char[] wordChars = word.ToCharArray(); Array.Sort(wordChars); return new string(wordChars); } } publicvoid Linq44() { string[] anagrams = {"from ", " salt", " earn ", " last ", " near ", " form "}; var orderGroups = anagrams.GroupBy(w => w.Trim(), new AnagramEqualityComparer()); ObjectDumper.Write(orderGroups, 1); } GroupBy - Comparer, Mapped public void Linq45() { string[] anagrams = {"from ", " salt", " earn ", " last ", " near ", " form "}; var orderGroups = anagrams.GroupBy( w => w.Trim(), a => a.ToUpper(), new AnagramEqualityComparer() ); ObjectDumper.Write(orderGroups, 1); } public class AnagramEqualityComparer : IEqualityComparer<string> { public bool Equals(string x, string y) { return getCanonicalString(x) == getCanonicalString(y); } public int GetHashCode(string obj) { return getCanonicalString(obj).GetHashCode(); } private string getCanonicalString(string word) { char[] wordChars = word.ToCharArray(); Array.Sort<char>(wordChars); return new string(wordChars); } } Set Operators Distinct - 1 publicvoid Linq46() { int[] factorsOf300 = { 2, 2, 3, 5, 5 }; var uniqueFactors = factorsOf300.Distinct(); Console.WriteLine("Prime factors of 300:"); foreach (var f in uniqueFactors) { Console.WriteLine(f); } } Distinct - 2 public void Linq47() { List products = GetProductList(); var categoryNames = ( from p in products select p.Category) .Distinct(); Console.WriteLine("Category names:"); foreach (var n in categoryNames) { Console.WriteLine(n); } } Union - 1 publicvoid Linq48() { int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; int[] numbersB = { 1, 3, 5, 7, 8 }; var uniqueNumbers = numbersA.Union(numbersB); Console.WriteLine("Unique numbers from both arrays:"); foreach (var n in uniqueNumbers) { Console.WriteLine(n); } } Union - 2 publicvoid Linq49() { List products = GetProductList();List customers = GetCustomerList(); var productFirstChars = from p in products select p.ProductName[0]; var customerFirstChars = from c in customers select c.CompanyName[0]; var uniqueFirstChars = productFirstChars.Union(customerFirstChars); Console.WriteLine("Unique first letters from Product names and Customer names:"); foreach (var ch in uniqueFirstChars) { Console.WriteLine(ch); } } Intersect - 1 publicvoid Linq50() { int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; int[] numbersB = { 1, 3, 5, 7, 8 }; var commonNumbers = numbersA.Intersect(numbersB); Console.WriteLine("Common numbers shared by both arrays:"); foreach (var n in commonNumbers) { Console.WriteLine(n); } } Intersect - 2 publicvoid Linq51() { List products = GetProductList(); List customers = GetCustomerList(); var productFirstChars = from p in products select p.ProductName[0]; var customerFirstChars = from c in customers select c.CompanyName[0]; var commonFirstChars = productFirstChars.Intersect(customerFirstChars); Console.WriteLine("Common first letters from Product names and Customer names:"); foreach (var ch in commonFirstChars) { Console.WriteLine(ch); } } Except - 1 public void Linq52() { int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; int[] numbersB = { 1, 3, 5, 7, 8 }; IEnumerable<int> aOnlyNumbers = numbersA.Except(numbersB); Console.WriteLine("Numbers in first array but not second array:"); foreach (var n in aOnlyNumbers) { Console.WriteLine(n); } } Except - 2 public void Linq53() { List products = GetProductList(); List customers = GetCustomerList(); var productFirstChars = from p in products select p.ProductName[0]; var customerFirstChars = from c in customers select c.CompanyName[0]; var productOnlyFirstChars = productFirstChars.Except(customerFirstChars); Console.WriteLine("First letters from Product names, but not from Customer names:"); foreach (var ch in productOnlyFirstChars) { Console.WriteLine(ch); } } Conversion Operators To Array public void Linq54() { double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 }; var sortedDoubles = from d in doubles orderby d descending select d; var doublesArray = sortedDoubles.ToArray(); Console.WriteLine("Every other double from highest to lowest:"); for (int d = 0; d < doublesArray.Length; d += 2) { Console.WriteLine(doublesArray[d]); } } To List public void Linq55() { string[] words = { "cherry", "apple", "blueberry" }; var sortedWords = from w in words orderby w select w; var wordList = sortedWords.ToList(); Console.WriteLine("The sorted word list:"); foreach (var w in wordList) { Console.WriteLine(w); } } To Dictionary public void Linq56() { var scoreRecords = new [] { new {Name = "Alice", Score = 50}, new {Name = "Bob" , Score = 40}, new {Name = "Cathy", Score = 45} }; var scoreRecordsDict = scoreRecords.ToDictionary(sr => sr.Name); Console.WriteLine("Bob's score: {0}", scoreRecordsDict["Bob"]); } OfType public void Linq57() { object[] numbers = { null, 1.0, "two", 3, 4.0f, 5, "six", 7.0 }; var doubles = numbers.OfType<double>(); Console.WriteLine("Numbers stored as doubles:"); foreach (var d in doubles) { Console.WriteLine(d); } } Element Operators First - Simple public void Linq58() { List products = GetProductList(); Product product12 = ( from p in products where p.ProductID == 12 select p ) .First(); ObjectDumper.Write(product12); } First - Indexed public void Linq60() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; int evenNum = numbers.First((num, index) => (num % 2 == 0) && (index % 2 == 0)); Console.WriteLine("{0} is an even number at an even position within the list.", evenNum); } FirstOrDefault - Simple public void Linq61() { int[] numbers = {}; int firstNumOrDefault = numbers.FirstOrDefault(); Console.WriteLine(firstNumOrDefault); } FirstOrDefault - Condition public void Linq62() { List products = GetProductList(); Product product789 = products.FirstOrDefault(p => p.ProductID == 789); Console.WriteLine("Product 789 exists: {0}", product789 != null); } FirstOrDefault - Indexed public void Linq63() { double?[] doubles = { 1.7, 2.3, 4.1, 1.9, 2.9 }; double? num = doubles.FirstOrDefault((n, index) => (n >= index - 0.5 && n <= index + 0.5)); if (num != null) Console.WriteLine("The value {1} is within 0.5 of its index position.", num); else Console.WriteLine("There is no number within 0.5 of its index position.", num); } ElementAt public void Linq64() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; int fourthLowNum = ( from n in numbers where n < 5 select n ) .ElementAt(3); // 3 because sequences use 0-based indexing Console.WriteLine("Fourth number < 5: {0}", fourthLowNum); } Generation Operators Range public void Linq65() { var numbers = from n in Sequence.Range(100, 50) selectnew {Number = n, OddEven = n % 2 == 1 ? "odd" : "even"}; foreach (var n in numbers) { Console.WriteLine("The number {0} is {1}.", n.Number, n.OddEven); } } Repeat public void Linq66() { var numbers = Sequence.Repeat(7, 10); foreach (var n in numbers) { Console.WriteLine(n); } } Quantifiers Any - Simple public void Linq67() { string[] words = { "believe", "relief", "receipt", "field" }; bool iAfterE = words.Any(w => w.Contains("ei")); Console.WriteLine("There is a word that contains in the list that contains 'ei': {0}", iAfterE); } Any - Indexed public void Linq68() { int[] numbers = { -9, -4, -8, -3, -5, -2, -1, -6, -7 }; bool negativeMatch = numbers.Any((n, index) => n == -index); Console.WriteLine("There is a number that is the negative of its index: {0}", negativeMatch); } Any - Grouped public void Linq69() { List products = GetProductList(); var productGroups = from p in products group p by p.Category into g where g.Group.Any(p => p.UnitsInStock == 0) select new {Category = g.Key, Products = g.Group}; ObjectDumper.Write(productGroups, 1); } All - Simple public void Linq70() { int[] numbers = { 1, 11, 3, 19, 41, 65, 19 }; bool onlyOdd = numbers.All(n => n % 2 == 1); Console.WriteLine("The list contains only odd numbers: {0}", onlyOdd); } All - Indexed public void Linq71() { int[] lowNumbers = { 1, 11, 3, 19, 41, 65, 19 }; int[] highNumbers = { 7, 19, 42, 22, 45, 79, 24 }; bool allLower = lowNumbers.All((num, index) => num < highNumbers[index]); Console.WriteLine("Each number in the first list is lower than its counterpart in the second list: {0}", allLower); } All - Grouped public void Linq72() { List products = GetProductList(); var productGroups = from p in products group p by p.Category into g where g.Group.All(p => p.UnitsInStock > 0) select new {Category = g.Key, Products = g.Group}; ObjectDumper.Write(productGroups, 1); } Aggregate Operators Count - Simple public void Linq73() { int[] factorsOf300 = { 2, 2, 3, 5, 5 }; int uniqueFactors = factorsOf300.Distinct().Count(); Console.WriteLine("There are {0} unique factors of 300.", uniqueFactors); } Count - Conditional public void Linq74() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; int oddNumbers = numbers.Count(n => n % 2 == 1); Console.WriteLine("There are {0} odd numbers in the list.", oddNumbers); } Count - Indexed public void Linq75() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; int oddEvenMatches = numbers.Count((n, index) => n % 2 == index % 2); Console.WriteLine("There are {0} numbers in the list whose odd/even status " + "matches that of their position.", oddEvenMatches); } Count - Nested public void Linq76() { List customers = GetCustomerList(); var orderCounts = from c in customers select new {c.CustomerID, OrderCount = c.Orders.Count()}; ObjectDumper.Write(orderCounts); } Count - Grouped public void Linq77() { List products = GetProductList(); var categoryCounts = from p in products group p by p.Category into g select new {Category = g.Key, ProductCount = g.Group.Count()}; ObjectDumper.Write(categoryCounts); } Sum - Simple public void Linq78() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; double numSum = numbers.Sum(); Console.WriteLine("The sum of the numbers is {0}.", numSum); } Sum - Projection public void Linq79() { string[] words = { "cherry", "apple", "blueberry" }; double totalChars = words.Sum(w => w.Length); Console.WriteLine("There are a total of {0} characters in these words.", totalChars); } Sum - Grouped public void Linq80() { List products = GetProductList(); var categories = from p in products group p by p.Category into g select new {Category = g.Key, TotalUnitsInStock = g.Group.Sum(p => p.UnitsInStock)}; ObjectDumper.Write(categories); } Min - Simple public void Linq81() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; int minNum = numbers.Min(); Console.WriteLine("The minimum number is {0}.", minNum); } Min - Projection public void Linq82() { string[] words = { "cherry", "apple", "blueberry" }; int shortestWord = words.Min(w => w.Length); Console.WriteLine("The shortest word is {0} characters long.", shortestWord); } Min - Grouped public void Linq83() { List products = GetProductList(); var categories = from p in products group p by p.Category into g select new {Category = g.Key, CheapestPrice = g.Group.Min(p => p.UnitPrice)}; ObjectDumper.Write(categories); } Min - Elements public void Linq84() { List products = GetProductList(); var categories = from p in products group p by p.Category into g from minPrice = g.Group.Min(p => p.UnitPrice) select new {Category = g.Key, CheapestProducts = g.Group.Where(p => p.UnitPrice == minPrice)}; ObjectDumper.Write(categories, 1); } Max - Simple public void Linq85() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; int maxNum = numbers.Max(); Console.WriteLine("The maximum number is {0}.", maxNum); } Max - Projection public void Linq86() { string[] words = { "cherry", "apple", "blueberry" }; int longestLength = words.Max(w => w.Length); Console.WriteLine("The longest word is {0} characters long.", longestLength); } Max - Grouped public void Linq87() { List products = GetProductList(); var categories = from p in products group p by p.Category into g select new {Category = g.Key, MostExpensivePrice = g.Group.Max(p => p.UnitPrice)}; ObjectDumper.Write(categories); } Max - Elements public void Linq88() { List products = GetProductList(); var categories = from p in products group p by p.Category into g from maxPrice = g.Group.Max(p => p.UnitPrice) select new {Category = g.Key, MostExpensiveProducts = g.Group.Where(p => p.UnitPrice == maxPrice)}; ObjectDumper.Write(categories, 1); } Average - Simple public void Linq89() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; double averageNum = numbers.Average(); Console.WriteLine("The average number is {0}.", averageNum); } Average - Projection public void Linq90() { string[] words = { "cherry", "apple", "blueberry" }; double averageLength = words.Average(w => w.Length); Console.WriteLine("The average word length is {0} characters.", averageLength); } Average - Grouped public void Linq91() { List products = GetProductList(); var categories = from p in products group p by p.Category into g select new {Category = g.Key, AveragePrice = g.Group.Average(p => p.UnitPrice)}; ObjectDumper.Write(categories); } Fold - Simple public void Linq92() { double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 }; double product = doubles.Fold((runningProduct, nextFactor) => runningProduct * nextFactor); Console.WriteLine("Total product of all numbers: {0}", product); } Fold - Seed public void Linq93() { double startBalance = 100.0; int[] attemptedWithdrawals = { 20, 10, 40, 50, 10, 70, 30 }; double endBalance = attemptedWithdrawals.Fold(startBalance, (balance, nextWithdrawal) => ( (nextWithdrawal <= balance) ? (balance - nextWithdrawal) : balance ) ); Console.WriteLine("Ending balance: {0}", endBalance); } Miscellaneous Operators Concat - 1 public void Linq94() { int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; int[] numbersB = { 1, 3, 5, 7, 8 }; var allNumbers = numbersA.Concat(numbersB); Console.WriteLine("All numbers from both arrays:"); foreach (var n in allNumbers) { Console.WriteLine(n); } } Concat - 2 public void Linq95() { List customers = GetCustomerList(); List products = GetProductList(); var customerNames = from c in customers select c.CompanyName; var productNames = from p in products select p.ProductName; var allNames = customerNames.Concat(productNames); Console.WriteLine("Customer and product names:"); foreach (var n in allNames) { Console.WriteLine(n); } } EqualAll - 1 public void Linq96() { var wordsA = new string[] { "cherry", "apple", "blueberry" }; var wordsB = new string[] { "cherry", "apple", "blueberry" }; bool match = wordsA.EqualAll(wordsB); Console.WriteLine("The sequences match: {0}", match); } EqualAll - 2 public void Linq97() { var wordsA = new string[] { "cherry", "apple", "blueberry" }; var wordsB = new string[] { "apple", "blueberry", "cherry" }; bool match = wordsA.EqualAll(wordsB); Console.WriteLine("The sequences match: {0}", match); } Custom Sequence Operators Combine public static class CustomSequenceOperators { public static IEnumerable Combine(this IEnumerable first, IEnumerable second, Func func) { using (IEnumerator e1 = first.GetEnumerator(), e2 = second.GetEnumerator()) { while (e1.MoveNext() && e2.MoveNext()) { yield return func(e1.Current, e2.Current); } } } } public void Linq98() { int[] vectorA = { 0, 2, 4, 5, 6 }; int[] vectorB = { 1, 3, 5, 7, 8 }; int dotProduct = vectorA.Combine(vectorB, (a, b) => a * b).Sum(); Console.WriteLine("Dot product: {0}", dotProduct); } Query Execution Deferred public void Linq99() { // Sequence operators form first-class queries that // are not executed until you enumerate over them. int[] numbers = new int[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; int i = 0; var q = from n in numbers select ++i; // Note, the local variable 'i' is not incremented // until each element is evaluated (as a side-effect): foreach (var v in q) { Console.WriteLine("v = {0}, i = {1}", v, i); } } Immediate public void Linq100() { // Methods like ToList() cause the query to be // executed immediately, caching the results. int[] numbers = new int[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; int i = 0; var q = ( from n in numbers select ++i ) .ToList(); // The local variable i has already been fully // incremented before we iterate the results: foreach (var v in q) { Console.WriteLine("v = {0}, i = {1}", v, i); } } Query Reuse public void Linq101() { // Deferred execution lets us define a query once // and then reuse it later after data changes. int[] numbers = new int[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var lowNumbers = from n in numbers where n <= 3 select n; Console.WriteLine("First run numbers <= 3:"); foreach (int n in lowNumbers) { Console.WriteLine(n); } for (int i = 0; i < 10; i++) { numbers[i] = -numbers[i]; } // During this second run, the same query object, // lowNumbers, will be iterating over the new state // of numbers[], producing different results: Console.WriteLine("Second run numbers <= 3:"); foreach (int n in lowNumbers) { Console.WriteLine(n); } } |
发表评论或回复