Home > Back-end >  Search an array in other array c#
Search an array in other array c#

Time:07-20

I have 2d array, Here is my array;

 public string[,,,] cars = new string[,,,]
{
    {
        { { "JOY", "Motor 1", "kırmızı", "link1" } },
        { { "JOY", "Motor 1", "mavi", "link" } },
        { { "JOY", "Motor 1", "siyah", "link" } },
        { { "JOY", "Motor 1", "gri", "link" } },
        { { "TOUCH", "Motor 1", "gri", "link" } }
    }
};

and 1d array,

  string[] selectedCar = new string[] { "JOY", "Motor 1", "gri" };

What I want to do is, I want to search the selectedCar array in the cars array and return the link which is the last elements of cars array.

Thanks in advance ^^

CodePudding user response:

To simplify this I would create an Car class like this:

[Serializable]
public class car {
   public string prop1;
   public string prop2;
   public string prop3;
   public string prop4;
}

and then just have a simple array or list of cars:

car[] cars = new car[]{...}

this way you just can use line to filter:

car res = cars.Where(x => x.prop1 == "value").First();

and now you can get any property of the filtered car.

You will need to add

using System.Linq;

on top of your script

  • Related