We help IT Professionals succeed at work.

linq .concat equivalent with expression syntax

Paweł
Paweł asked
on
396 Views
Last Modified: 2017-03-30
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.
Comment
Watch Question

PawełI Design & Develop Software

Author

Commented:
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.
Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
PawełI Design & Develop Software

Author

Commented:
Thanks very much
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Any time Paweł,glad to help.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.