• 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 }
  • 相关阅读:
    Exp8 Web综合
    Exp7 网络欺诈防范
    更新Linux下openssl路径和库路径
    CVE-2019-5786漏洞利用复现
    Exp6 MSF应用基础
    CVE-2020-11651漏洞利用复现
    Exp5 信息搜集与漏洞扫描
    Exp4 恶意代码分析
    快速搭建SpringBoot项目
    Java split() 方法注意事项
  • 原文地址:https://www.cnblogs.com/David-Huang/p/4256120.html
Copyright © 2020-2023  润新知