Arlita Nurmaya Asri
Arlita Nurmaya Asri

Reputation: 81

How to find index of sublist without class C#

I want to find the index of sublist in list route which contains certain value(i), but I don't want to make a class of it. Here is my code:

var route = new List<List<int>>();
for (int i = 0; i<DC1; ++i)
{
    for (int j = 0; j<DC1; ++j)
    {   
        if (routeopt.x[0, j] == 1)
        {
            List<int> subroute = new List<int>();                        

            if (routeopt.x[i, j] == 1)
            { 
                subroute.Add(j);
                route.Add(subroute);
            }
        }
    }

    if(i == 0) continue;

    for (int j = 1; j<DC1;j++ )
    {
        if (routeopt.x[i, j] == 1)
        route[route.IndexOf(i)].Add ( j);
    }
}

foreach (var subroute in route)
{
    Console.Write("subroute: ");
    foreach (int s in subroute)
        Console.Write(s + " ");
        Console.WriteLine();
}

For example, based on this code:

for (int j = 1; j < DC1;j++ )
{
     if (routeopt.x[i, j] == 1)
     route[route.IndexOf(i)].Add(j);
}

I want to make if x[1,3] == 1 then I can add 3 to sublist which contains 1. this code route.IndexOf(i) is still get red underline, please help how to correct it. Thanks

Upvotes: 3

Views: 313

Answers (2)

Ghasan غسان
Ghasan غسان

Reputation: 5877

You can use LINQ's Single method to retrieve the specific list you want given a predicate Contains(i). Here I am looking for the list that contains 6, and adds 7 to it.

 var routes = new List<List<int>>()
 {
   new List<int>() {1, 2, 3},
   new List<int>() {4, 5, 6},   
 };

 List<int> targetList = routes.Single(i => i.Contains(6));
 targetList.Add(7);

To get specifically the index of that list, then you can use IndexOf method like:

 int targetListIndex = routes.IndexOf(targetList); // 1 in this example

Upvotes: 2

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186813

Let's start from an example (which we turn into a test later):

 List<List<int>> route = new List<List<int>>() {
   new List<int>() {1, 2, 3, 4, 5}, // sublist #0: contains value == 4
   new List<int>() {7, 8, 2, 9},    // sublist #1: doesn't contain value == 4
   new List<int>() {9, 10, 4},      // sublist #2: contains value == 4
 };

and we are looking for value within each of sublists

 int value = 4;

finally, we want to have sublist indexes as the outcome: 0 and 2. If it's your case, I suggest using Linq

List<List<int>> route = new List<List<int>>() {
  new List<int>() {1, 2, 3, 4, 5}, 
  new List<int>() {7, 8, 2, 9},    
  new List<int>() {9, 10, 4}, 
};

int value = 4;

var indexesFound = route
  .Select((sublist, index) => new { // eh, new class, but anonymous one
     sublist = sublist,
     index = index, }) 
  .Where(chunk => chunk.sublist.Contains(value))
  .Select(chunk => chunk.index)
  .ToArray(); // if you want, say, array materialization

Test

Console.Wrire(string.Join(", ", indexesFound));

Outcome:

0, 2

Edit: if you want to have just one index, you have to specify which one, e.g. for the very first index put .First() instead of .ToArray():

int firstIndexFound = route
  .Select((sublist, index) => new { // eh, new class, but anonymous one
     sublist = sublist,
     index = index, }) 
  .Where(chunk => chunk.sublist.Contains(value))
  .Select(chunk => chunk.index)
  .First(); // or .Last()

Upvotes: 2

Related Questions