Mixxiphoid
Mixxiphoid

Reputation: 1034

How to select certain strings (by index) from a list in a list with linq?

I have an object that looks like this.

List<List<string>> MyData;

The inner List<string> can contain an undefined number of strings but the number is always larger than 0.
In an other object I have the index numbers of the strings I want to select. The Index number is located in MyObject.Number;

How can I get a List<List<string>> object with only the strings present at the given indexes?

I tried the following:

List<List<string>> test = new List<List<string>>();
List<MyObject>.ForEach(p => test.Add(MyData.Select(q => q[p.Number]).ToList()));

This didn't worked out well since I got a list with the string seperate in lists.

Example:
I got a List with 100 List<string> in it. The List<string> contains 5 strings. List<MyObject> tells me he has the indexes 0 and 2.
The code I tried returned to me a list with two lists in it. The first list contained all the strings in 0 location of List<string>, the second list all the strings in the 2 location of List<string>. I wanted a list with 100 lists and in every list 2 strings.

Please help me to solve this problem.

Feel free to ask more information if needed.

SOLUTION:

List<List<string>> MyResult = MyData.Select(l => MyObjects.Select(p => p.Number).ToList().Select(i => l[i]).ToList()).ToList();

Upvotes: 2

Views: 14063

Answers (4)

Khairuddin Ni&#39;am
Khairuddin Ni&#39;am

Reputation: 833

var indexes = myObjects.Select(obj => obj.Number);
var result = test.Select(list => indexes.Select(idx => list[idx]).ToList()).ToList();

myObjects is list of MyObject instance.

Upvotes: 0

Kevin Gosse
Kevin Gosse

Reputation: 39007

        var lists = new List<List<string>>
        {
            new List<string> { "1", "2", "3", "4" },
            new List<string> { "5", "6", "7", "8" },
            new List<string> { "9", "10", "11", "12" },
        };

        var indexes = new List<int> { 0, 2 };

        var result = lists.Select(l => indexes.Select(i => l[i]).ToList()).ToList();

        foreach (var list in result)
        {
            Console.Write("list:");

            foreach (var elt in list)
            {
                Console.Write(elt + ",");
            }

            Console.Write(" / ");
        }

Output:

list:1,3, / list:5,7, / list:9,11, /

Upvotes: 1

Jarek
Jarek

Reputation: 3379

In linq you can pass two parameters, the second one will be integer with index number of current element.

List<string> strings = new List<string>();
strings.Where((s, i) => i == indexToLookFor).Select(s => s);

This way you can easily return only items with indexes you are looking for. In your case you can use something like this:

strings.Where((s, i) => indexes.Contains(i)).Select(s => s);

Of course this is just an example, but I'm sure you can adjust it to fit to your case.

Upvotes: 2

vc 74
vc 74

Reputation: 38179

I think this is what you want (indexes is the collection of requested indexes)

var indexes = new List<int>() {0, 2};
var whatYouWant = strings.Select(item => indexes.Select(index => item[index]).ToList())

Upvotes: 4

Related Questions