LINQ Samples(Set Samples)

孙嘉悦
2023-12-01

Distinct - 1


This sample prints the prime factors of the number 300. The sample creates a sequence of unique factors by using the Distinct method on the integer array of factors to remove duplicates.
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);
    }
}
Result
Prime factors of 300:
2
3
5

Distinct - 2


This sample prints the unique Category names by first selecting all of the category names from the product list, then using Distinct to remove duplicate names.
publicvoid 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);
    }
}
Result
Category names:
Beverages
Condiments
Produce
Meat/Poultry
Seafood
Dairy Products
Confections
Grains/Cereals

Union - 1


This sample prints the unique elements of two integer arrays. The sample uses the Union method to create a sequence that is a union of the two integer arrays with duplicate elements removed.
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);
    }
}
Result
Unique numbers from both arrays:
0
2
4
5
6
8
9
1
3
7

Union - 2


This sample prints unique letters from Product and Customer names. The sample creates a sequence of product first characters, a sequence of customer first characters, then joins the two sets by using Union to merge them and remove duplicates.
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);
    }
}
Result
Unique first letters from Product names and Customer names:
C
A
G
U
N
M
I
Q
K
T
P
S
R
B
J
Z
V
F
E
W
L
O
D
H

Intersect - 1


This sample prints a list of numbers that are common to two integer arrays. The sample uses Intersect to create one sequence that contains the common values shared by both arrays.
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);
    }
}
Result
Common numbers shared by both arrays:
5
8

Intersect - 2


This sample prints the letters that are both the first letter of a Product name and the first letter of a Customer name. The sample uses query statements to create two sequence - the first letters of Product names and the first letter of Customer names, then uses Intersect to create a sequence of letters common to both.
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);
    }
}
Result
Common first letters from Product names and Customer names:
C
A
G
N
M
I
Q
K
T
P
S
R
B
V
F
E
W
L
O

Except - 1

This sample prints numbers that are in one integer array, but not another. The sample uses Except to create a sequence that contains the values from numbersA that are not also in numbersB.

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);
    }
}

Result

Numbers in first array but not second array:
0
2
4
6
9


Except - 2

This sample prints the first character of product names that aren't also the first character of customer names. After getting the first characters of product and customer names using query expressions, the sample uses Except to create a sequence of product characters that doesn't include first characters of customer names.

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);
    }
}

Result

First letters from Product names, but not from Customer names:
U
J
Z

 
 类似资料:

相关阅读

相关文章

相关问答