Linq101-Set

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4
  5 namespace Linq101
  6 {
  7     class Set
  8     {
  9         /// <summary>
 10         /// This sample uses Distinct to remove duplicate elements in a sequence of factors of 300.
 11         /// </summary>
 12         public void Linq46()
 13         {
 14             int[] factorsOf300 = { 2, 2, 3, 5, 5 };
 15
 16             var uniqueFactors = factorsOf300.Distinct();
 17
 18             Console.WriteLine("300的因数:");
 19             foreach (var factor in uniqueFactors)
 20             {
 21                 Console.WriteLine(factor);
 22             }
 23         }
 24
 25         /// <summary>
 26         /// This sample uses Distinct to find the unique Category names.
 27         /// </summary>
 28         public void Linq47()
 29         {
 30             List<Data.Product> products = Data.GetProductList();
 31
 32             var categoryNames = (from p in products
 33                                  select p.Category).Distinct();
 34
 35             Console.WriteLine("种类名称:");
 36             ObjectDumper.Write(categoryNames);
 37         }
 38
 39         /// <summary>
 40         /// This sample uses Union to create one sequence that contains the unique values from both arrays.
 41         /// </summary>
 42         public void Linq48()
 43         {
 44             int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
 45             int[] numbersB = { 1, 3, 5, 7, 8 };
 46
 47             var uniqueNumbers = numbersA.Union(numbersB);
 48
 49             Console.WriteLine("Unique numbers from both arrays:");
 50             ObjectDumper.Write(uniqueNumbers);
 51         }
 52
 53         /// <summary>
 54         /// This sample uses Union to create one sequence that contains the unique first letter from both product and customer names.
 55         /// </summary>
 56         public void Linq49()
 57         {
 58             List<Data.Product> products = Data.GetProductList();
 59             List<Data.Customer> customers = Data.GetCustomerList();
 60
 61             var productFirstChar = from p in products select p.ProductName[0];
 62             var customerFirstChar = from c in customers select c.CompanyName[0];
 63
 64             var uniqueFirstChar = productFirstChar.Union(customerFirstChar);
 65
 66             Console.WriteLine("Unique First Char:");
 67             ObjectDumper.Write(uniqueFirstChar);
 68         }
 69
 70         /// <summary>
 71         /// This sample uses Intersect to create one sequence that contains the common values shared by both arrays.
 72         /// </summary>
 73         public void Linq50()
 74         {
 75             int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
 76             int[] numbersB = { 1, 3, 5, 7, 8 };
 77
 78             var commonNumbers = numbersA.Intersect(numbersB);
 79
 80             Console.WriteLine("共有的数字:");
 81             ObjectDumper.Write(commonNumbers);
 82         }
 83
 84         /// <summary>
 85         /// This sample uses Intersect to create one sequence that contains the common first letter from both product and customer names.
 86         /// </summary>
 87         public void Linq51()
 88         {
 89             List<Data.Product> products = Data.GetProductList();
 90             List<Data.Customer> customers = Data.GetCustomerList();
 91
 92             var productFirstChar = from p in products select p.ProductName[0];
 93             var customerFirstChar = from c in customers select c.CompanyName[0];
 94
 95             var commonFirstChar = productFirstChar.Intersect(customerFirstChar);
 96
 97             Console.WriteLine("Common First Char");
 98             ObjectDumper.Write(commonFirstChar);
 99         }
100
101         /// <summary>
102         /// This sample uses Except to create a sequence that contains the values from numbersAthat are not also in numbersB.
103         /// </summary>
104         public void Linq52()
105         {
106             int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
107             int[] numbersB = { 1, 3, 5, 7, 8 };
108
109             var aOnlyNumbers = numbersA.Except(numbersB);
110
111             Console.WriteLine("Numbers in arrayA but no ArrayB");
112             ObjectDumper.Write(aOnlyNumbers);
113         }
114
115         /// <summary>
116         /// This sample uses Except to create one sequence that contains the first letters of product names that are not also first letters of customer names.
117         /// </summary>
118         public void Linq53()
119         {
120             List<Data.Product> products = Data.GetProductList();
121             List<Data.Customer> customers = Data.GetCustomerList();
122
123             var productFirstChar = from p in products select p.ProductName[0];
124             var customerFirstChar = from c in customers select c.CompanyName[0];
125
126             var productOnlyFirstChar = productFirstChar.Except(customerFirstChar);
127
128             Console.WriteLine("First char only in productFirstChar");
129             ObjectDumper.Write(productOnlyFirstChar);
130         }
131     }
132 }
时间: 2024-10-10 16:28:46

Linq101-Set的相关文章

LINQ 101——约束、投影、排序

什么是LINQ:LINQ 是一组 .NET Framework 扩展模块集合,内含语言集成查询.集合以及转换操作.它使用查询的本机语言语法来扩展 C# 和 Visual Basic,并提供利用这些功能的类库. 什么是LINQ 101:是学习LINQ的不错的资源(下载地址:http://pan.baidu.com/s/1ntE74NJ),当初我也是看LINQ101来熟悉LINQ的.现把我当时的学习笔记整理出来 Restriction (约束)Projection (投影)Ordering (排序)

101个LINQ例子

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:");    foreac

LINQ 的查询执行何时是延迟执行,何时是立即执行,以及查询的复用

延迟执行的经典例子: 我们用 select ++i 就可以看到在foreach 时候,查询才被执行. public static void Linq99(){    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;    foreach (var v in q)        Console.WriteLine("v

Grouping Operators

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 5 namespace Linq101 6 { 7 class Grouping 8 { 9 /// <summary> 10 /// This sample uses group by to partition a list of numbers by their remainder when divided by 5. 11 /// <

Linq101-Ordering

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 5 namespace Linq101 6 { 7 class Ordering 8 { 9 /// <summary> 10 /// This sample uses orderby to sort a list of words alphabetically. 11 /// </summary> 12 public void Li

Linq101-CustomSequence

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 5 namespace Linq101 6 { 7 class CustomSequence 8 { 9 public void Linq98() 10 { 11 int[] vectorA = { 0, 2, 4, 5, 6 }; 12 int[] vectorB = { 1, 3, 5, 7, 8 }; 13 14 int result = v

Linq101-Conversion Operators

1 using System; 2 using System.Linq; 3 4 namespace Linq101 5 { 6 class Conversion 7 { 8 /// <summary> 9 /// This sample uses ToArray to immediately evaluate a sequence into an array. 10 /// </summary> 11 public void Linq54() 12 { 13 double[] d

C#学习记录7——LINQ

LINQ(Language-INtegrated Query)语言集成查询,可以帮助我们将查询功能添加到C#中.可用于数据库等检索. 声明Query十分简单,语法上与SQL语言十分相似,只是有少许的区别 关于LINQ,在msdn上有101 Samples十分详细介绍解释LINQ如何使用在具体工程中.链接:https://code.msdn.microsoft.com/101-linq-samples-3fb9811b 一.Query Execution 查询的执行 介绍了Query执行中的特点

Linq101-Restriction

1 using System; 2 using System.Linq; 3 4 namespace Linq101 5 { 6 class Restriction 7 { 8 /// <summary> 9 /// This sample uses where to find all elements of an array less than 5. 10 /// </summary> 11 public void Simple1() 12 { 13 int[] numbers