0

Is there a short form (maybe using LINQ) for making integers to objects and adding them into a List?

I imagine maybe something like List<Car> TestList = car1.Neighbors.To(c => cars[c]);

using System;
using System.Collections.Generic;                   
public class Program
{
    public static void Main()
    {
        // Cars
        Car car0 = new Car(0, new List<int> { 1 });
        Car car1 = new Car(1, new List<int> { 0, 2 });
        Car car2 = new Car(2, new List<int> { 1 });
        List<Car> cars = new List<Car> { car0, car1, car2 };

        // THIS I WANT TO SHORTEN ▼▼▼▼▼▼▼▼▼▼
        List<Car> TestList = new List<Car>();
        foreach (int i in car1.Neighbors)
            TestList.Add(cars[i]);
        // ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲

        Console.Write("Neighbors of car 1:");
        foreach (Car car in TestList)
            Console.Write(" car" + car.Index);
    }   

    public class Car
    {
        public int Index; // index of the car
        public List<int> Neighbors; // car indexes, that are parked near to this car
        public Car (int index, List<int> neighbors)
        {
            Index = index;
            Neighbors = neighbors;
        }
    }
}
Pixel_95
  • 954
  • 2
  • 9
  • 21
  • 1
    linq is not much more than a glorified for loop, why specifically linq? what exactly are you struggling with, why are you wishing to turn ints into objects – BugFinder Oct 22 '19 at 07:24

3 Answers3

3

You should use Enumerable.Select (from System.Linq) which projects each element of a sequence into a new form (https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.select?view=netframework-4.8)

IEnumerable<Car> TestList = car1.Neighbors.Select(i => cars[i]);

or this if you absolutely need a List

List<Car> TestList = car1.Neighbors.Select(n => cars[n]).ToList();
An0d
  • 213
  • 2
  • 12
  • thanks for your answer! What is the advantage of an IEnumerable instead of a List? – Pixel_95 Oct 22 '19 at 07:36
  • Question already asked ;-), please see https://stackoverflow.com/questions/3628425/ienumerable-vs-list-what-to-use-how-do-they-work – An0d Oct 22 '19 at 07:38
2

I think you're asking about something like car1.Neighbors.Select(x => cars[i]).ToList(), however; frankly, I would suggest storing a list of references rather than a list of indices - unless you have very specific needs (usually related to advanced indexing strategies). A List<Car> as Neighbors would work much more directly, and avoids a lot of problems. If you're on x86, it won't even cost anything extra; on x64, yes a 64-bit reference is a little bigger than a 32-bit integer, but: you're avoiding all the indirection and all the problems of index management. With that, this code becomes literally:

var neighbors = car1.Neighbors;
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thanks for the general hint! Maybe i implement this in my code! – Pixel_95 Oct 22 '19 at 07:37
  • Is this solution also practicable, if I want to delete or find a certain car from the list? maybe I want to delete car2 from the neighbor list of car1. How can I do this in your solution? In case, I have to search for the `car.index`, then I don't see an advantage for me – Pixel_95 Oct 22 '19 at 10:48
  • @Pixel_95 `car1.Neighbors.Remove(car2)` – Marc Gravell Oct 22 '19 at 10:53
1
List<Car> TestList = new List<Car>(car1.Neighbors.Select(x => cars[x]));

Or

List<Car> TestList = new List<Car>(cars.Where(x => car1.Neighbors.Contains(x.Index)));
Innat3
  • 3,561
  • 2
  • 11
  • 29
  • 1
    @LasseVågsætherKarlsen how come? I mean I know the "Index" property is not the same as the position in the array, but seeing OP's pattern it should work – Innat3 Oct 22 '19 at 07:29