Partilhar via


LINQ A Journey through Operators [Concat]

 

Imagine you have two arrays and you need to join them. Using LINQ it is as simple as mentioned

using System;

using System.Collections.Generic;

using System.Text;

using System.Query;

using System.Xml.XLinq;

using System.Data.DLinq;

namespace LINQ_Concat

{

    class Program

    {

        static void Main(string[] args)

        {

            char[] alpha1 = {'A','B','C','D','E','F','G','H','I'};

            char[] alpha2 = {'J','K','L','M','N','O','P','Q','R'};

  var alphaAll = alpha1.Concat(alpha2);

            foreach(var al in alphaAll)

            {

                Console.WriteLine(al);

            }

            Console.ReadKey();

        }

    }

}

Output will look like

A

B

C

D

E

F

G

H

I

J

K

L

M

N

O

P

Q

R

Namoskar

Comments

  • Anonymous
    December 11, 2006
    Is Concat usable as a static method as well? I would rather write Concat(alpha1, alpha2). I know that the extension methods are defined as static methods, but are they also usable as such or will C# enforce the above syntax?

  • Anonymous
    March 01, 2008
    What is cool... is that you can order at the same time... If your letters were jumbled, you could write var alphaAll = alpha1.Concat(alpha2).OrderBy(a => a); And you'd sort and concat them at the same time!!!