| by Arround The Web | No comments

C# LINQ Concat() Method

If you want to append the second data source to the first data source, LINQ in C# provides a method – Concat().

Language Integrated Query language (LINQ) is used to perform operations on the C# collections or Normal data structures. We can also perform the database query operations with LINQ. LINQ supports many methods and functions that are used for data manipulation like Updation, deletion and Insertion, etc.

LINQ Concat()

Concat() in LINQ joins the two data sources. The data source can be an Array, List, HashSet, etc. But the condition is that both the data types are similar. Otherwise, error occurs.

Syntax

input_source1.Concat(input_source2);

Where input_source1 is the first data source and input_source2 is the second data source.

We will further understand this by the following examples:

Example 1

Here, we will create two lists with int type that have numeric values and concatenate these two lists.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{
 
    static public  void Main(){
       
       //create List named input_numbers1
       var input_numbers1 = new  List() {100,200,300,456};
     
  //create List named input_numbers2
       var input_numbers2 = new  List() {1,2,3,4};
       
//apply the LINQ Concat() method to join two data sources.
       var combined = input_numbers1.Concat(input_numbers2);
 
        Console.WriteLine("Combined Data:");
        foreach(var values in combined)
        {
            Console.WriteLine(values);
        }
    }
           
}

Output

Explanation
1. First, we created two lists with Integer types. The input_numbers1 refers to the first Integer List and the input_numbers2 refers to the second  Integer List.

2. After that, we joined two lists using the Concat() method.

3. Finally, we are display the Output using a foreach loop.

Example 2

Here, we will create two lists with string types that have string values and concatenate these two lists.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{
 
    static public  void Main(){
       
       //create List named input_strings1
       var input_strings1 = new  List() {"Hello"};
     
  //create List named input_strings2
       var input_strings2 = new  List() {"Linuxhint","welcomes","U"};
       
//apply the LINQ Concat() method to join two data sources.
       var combined = input_strings1.Concat(input_strings2);
 
        Console.WriteLine("Combined Data:");
        foreach(var values in combined)
        {
            Console.WriteLine(values);
        }
    }
           
}

Output

Explanation
1. First, we created two lists with String types. The input_strings1 refers to the first String List and the input_strings2 refers to the second String List.

2. After that, we joined the two lists using the Concat() method.

3. Finally, we are displayed the Output using a foreach loop.

Example 3

Let’s create the Food that holds three attributes – food_price, name, and quantity. Then, we create two lists from the Food Source and concatenate the name attribute.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{
    //define the data for Food
   class Food  
    {  
        public int food_price { get; set; }  
        public string name { get; set; }  
        public int quantity { get; set; }  
    }
    static public  void Main(){
       
       //create data
       List first_list = new List();
       //add values
       first_list.Add(new Food { food_price=300,name="parota",quantity=1 });
       first_list.Add(new Food { food_price=800,name="paneer",quantity=4 });
       first_list.Add(new Food { food_price=100,name="mushroom",quantity=2 });
       first_list.Add(new Food { food_price=564,name="vegtables",quantity=10 });
       first_list.Add(new Food { food_price=400,name="fruits",quantity=8 });
       
           
            //create data
       List second_list = new List();
       //add values
       second_list.Add(new Food { food_price=44,name="roti",quantity=0 });
       second_list.Add(new Food { food_price=44,name="chocos",quantity=1});
       second_list.Add(new Food { food_price=12,name="ice-cream",quantity=2 });
       
       Console.WriteLine("--------------------------List=I--------------------------");
       
       foreach (var value in first_list)  
            {  
                Console.WriteLine(value.food_price+"->"+value.name+"->"+value.quantity);  
            }  
           
         Console.WriteLine("--------------------------List=II--------------------------");
         foreach (var value in second_list)  
            {  
                Console.WriteLine(value.food_price+"->"+value.name+"->"+value.quantity);  
            }
           
        Console.WriteLine("--------------------------LINQ Concat - name--------------------------");

         //concatenate the name attribute in both the lists.
       var result = first_list.Select(element => element.name).Concat(second_list.Select(element => element.name));
   
             
       foreach (var value in result)  
            {  
                Console.WriteLine(value);  
            }
           
           
}
}

Output

Explanation
1. We created two lists from Food named first_list and second_list. The first_list has 5 values and the second_list has 3 values.

2. After that, we combined the name attribute values from the two lists using the Concat() method.

3. Finally, we are displayed the combined name attribute using the foreach loop.

Conclusion

We learned how to join the two data sources using the Concat() method available in C# – LINQ. Make sure that the data types must be the same while concatenating the two data sources. We demonstrated three different examples to understand the concept better and used the modules using System, using System.Linq, using System.Collections.Generic in your code.

Share Button

Source: linuxhint.com

Leave a Reply