Avatar of Paweł
PawełFlag for Switzerland

asked on 

linq .concat equivalent with expression syntax

So i'm trying to familiarize myself with the expression syntax of linq, you know the upside down SQL stuff; it's a bit of a change from the method based which i'm used to, but not too bad; however i can't for the life of me figure out the equivalent of the following

class Program
{
    static void Main(string[] args)
    {
        var nums = Enumerable.Range(0, 10);
        var chars = "Hello world".ToCharArray();
        var names = new string[] {"Pawel", "Magda", "Tomek" };

        var result = nums.Select(n => n.ToString()).Concat(names).Concat(chars.Select(c => c.ToString()));

        Array.ForEach(result.ToArray(), s => Console.Write(s + ", "));
        Console.WriteLine();
    }
}

Open in new window


in the expression syntax.
C#LINQ Query

Avatar of undefined
Last Comment
Fernando Soto
Avatar of Paweł
Paweł
Flag of Switzerland image

ASKER

So this mixed syntax is the best i've come up with

class Program
{
    static void Main(string[] args)
    {
        var nums = Enumerable.Range(0, 10);
        var chars = "Hello world".ToCharArray();
        var names = new string[] { "Pawel", "Magda", "Tomek" };

        var result1 = (from n in nums
                        select n.ToString())
                        .Concat(from c in chars
                                select c.ToString())
                        .Concat(names);

        Array.ForEach(result1.ToArray(), n => Console.Write(n + ", "));
        Console.WriteLine();


        var result2 = nums.Select(n => n.ToString())
            .Concat(chars.Select(c => c.ToString()))
            .Concat(names);

        Array.ForEach(result2.ToArray(), s => Console.Write(s + ", "));
        Console.WriteLine();
    }
}

Open in new window


if someone can come up with a pure expression based solution, please share, there's no urgency this is a contrived example.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Paweł
Paweł
Flag of Switzerland image

ASKER

Thanks very much
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Any time Paweł,glad to help.
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo