📜  LINQ |串联运算符|康卡特

📅  最后修改于: 2021-05-29 17:24:13             🧑  作者: Mango

串联是将一个序列附加到另一个序列的过程。在LINQ中,串联操作仅包含一个称为Concat的运算符。它用于附加两种相同类型的序列或集合并返回新的序列或集合。

  • 它不支持C#和VB.NET语言的查询语法。
  • 它支持C#和VB.NET语言的方法语法。
  • 它同时存在于Queryable和Enumerable类中。
  • 它是通过使用延迟执行来实现的。
  • 它允许重复的元素。

如下图所示,此处将两个相同类型的序列连接为一个序列。

范例1:

// C# program to concatenate the
// given sequences
using System;
using System.Linq;
  
class GFG {
  
    static public void Main()
    {
  
        // Data source
        char[] sequence1 = {'p', 'q', 'r', 's', 'y', 'z'};
        char[] sequence2 = {'p', 'm', 'o', 'e', 'c', 'z'};
  
        // Display the sequences
        Console.WriteLine("Sequence 1 is: ");
  
        foreach(var s1 in sequence1)
        {
            Console.WriteLine(s1);
        }
  
        Console.WriteLine("Sequence 2 is: ");
  
        foreach(var s2 in sequence2)
        {
            Console.WriteLine(s2);
        }
  
        // Concatenate the given array
        // Using Concat function
        var result = sequence1.Concat(sequence2);
  
        Console.WriteLine("New Sequence:");
        foreach(var val in result)
        {
            Console.WriteLine(val);
        }
    }
}
输出:
Sequence 1 is: 
p
q
r
s
y
z
Sequence 2 is: 
p
m
o
e
c
z
New Sequence:
p
q
r
s
y
z
p
m
o
e
c
z

范例2:

// C# program to print the major 
// language of the employees
using System;
using System.Linq;
using System.Collections.Generic;
  
// Employee details
public class Employee1 {
  
    public int emp_id1
    {
        get;
        set;
    }
  
    public string emp_name1
    {
        get;
        set;
    }
    public string emp_lang1
    {
        get;
        set;
    }
}
  
// Employee details
public class Employee2 {
  
    public int emp_id2
    {
        get;
        set;
    }
  
    public string emp_name2
    {
        get;
        set;
    }
    public string emp_lang2
    {
        get;
        set;
    }
}
  
class GFG {
  
    // Main method
    static public void Main()
    {
        List emp1 = new List() {
  
            new Employee1() {emp_id1 = 209, emp_name1 = "Anjita",
                                               emp_lang1 = "C#"},
  
            new Employee1() {emp_id1 = 210, emp_name1 = "Soniya",
                                                emp_lang1 = "C"},
  
            new Employee1() {emp_id1 = 211, emp_name1 = "Rohit",
                                            emp_lang1 = "Java"},
  
        };
  
        List emp2 = new List() {
  
            new Employee2() {emp_id2 = 219, emp_name2 = "Anita",
                                           emp_lang2 = "Scala"},
  
            new Employee2() {emp_id2 = 223, emp_name2 = "Manya",
                                          emp_lang2 = "Python"},
  
            new Employee2() {emp_id2 = 266, emp_name2 = "Rohan",
                                            emp_lang2 = "Ruby"},
  
        };
  
        // Query to concatenate the major
        // languages of the employees of 
        // two different departments
        // Using Concat method
        var res = emp1.Select(e => e.emp_lang1).Concat(emp2.Select(e => e.emp_lang2));
  
        Console.WriteLine("Major Languages Used are: ");
          
        foreach(var val in res)
        {
            Console.WriteLine(val);
        }
    }
}
输出:
Major Languages Used are: 
C#
C
Java
Scala
Python
Ruby