Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In LINQ, can I select multiple items?

Tags:

c#

.net

linq

Say I have an array of strings like this:

string [] foos = {
    "abc",
    "def",
    "ghi"
};

I want a new collection that contains each string and its reverse. So the result should be:

    "abc",
    "cba",
    "def",
    "fed",
    "ghi",
    "ihg"

I could just iterate through the array, but this is pretty clumsy:

string Reverse (string str)
{
    return new string (str.Reverse ().ToArray ());
}

List<string> results = new List<string> ();
foreach (string foo in foos) {
    results.Add (foo);
    results.Add (Reverse(str));
}

Is there a way to do this in LINQ? Something like

var results = from foo in foos
    select foo /* and also */ select Reverse(foo)
like image 664
Matthew Avatar asked Oct 03 '10 16:10

Matthew


People also ask

How do you Select two columns in LINQ?

Select multiple columns using Entity Framework You can select to an anonymous type, for example... var dataset2 = (from recordset in entities. processlists where recordset. ProcessName == processname select new { serverName = recordset.

How does Select work in LINQ?

Select is used to project individual element from List, in your case each customer from customerList . As Customer class contains property called Salary of type long, Select predicate will create new form of object which will contain only value of Salary property from Customer class.

Can LINQ query work with array?

Yes it supports General Arrays, Generic Lists, XML, Databases and even flat files. The beauty of LINQ is uniformity. Now, how does it provide uniformity? Using a single LINQ query you can process data in various data sources.


1 Answers

var result = from foo in foos
             from x in new string[] { foo, Reverse(foo) }
             select x;

or

var result = foos.SelectMany(foo => new string[] { foo, Reverse(foo) });

This maps each foo in foos to an array which consists of two elements: foo and Reverse(foo). Then it concatenates all these two-element arrays into a single, large enumerable.

{                           {                             {
"abc",                          {
                                "abc",                    "abc",
                                "cba",                    "cba",
                                },
"def",           =>             {              =>
                                "def",                    "def",
                                "fed",                    "fed",
                                }
"ghi",                          {
                                "ghi",                    "ghi",
                                "ihg",                    "ihg",
                                }
}                           }                             }

If the order of your output is not so important, you can also concatenate the original array with the result of mapping each element in the original array using the Reverse method:

var result = foos.Concat(from foo in foos select Reverse(foo));

or

var result = foos.Concat(foos.Select(Reverse));
{                               {                               {
"abc",                          "abc",                          "abc",
"def",           =>             "def",           =>             "def",
"ghi",                          "ghi",                          "ghi",
}                               }
                                concat
                                {
                                "cba",                          "cba",
                                "fed",                          "fed",
                                "ihg",                          "ihg",
                                }                               }

like image 63
dtb Avatar answered Sep 28 '22 18:09

dtb