2

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();

4 Answers 4

4

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())
Sign up to request clarification or add additional context in comments.

2 Comments

This answer is wrong. It selects just char 0 and 2 of each string in the list, but not the string 0 and 2 of the list by index.
@monotomy I don't think it's wrong, don't forget that <code>strings</code> contains a list of list of strings, not a list of strings
2

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.

Comments

1
        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, /

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.