LINQ: Distinct

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
	public static List<Car> Cars = new List<Car>();
	static void Main(string[] args)
	{
		DisplayDataFromFile();
	}

	private static void DisplayDataFromFile()
	{
		Cars = CarDataService.GetCars();
		List<string> carMakes = Cars.Select(c => c.Make).Distinct().ToList();
		Console.WriteLine("CARS MAKES:");
		foreach (var makes in carMakes)
		{
			Console.WriteLine($"Make: {makes}");
		}
	}

	public class Car
	{
		public int OwnerID
		{
			get;
			set;
		}

		public int Year
		{
			get;
			set;
		}

		public string Make
		{
			get;
			set;
		}

		public string Model
		{
			get;
			set;
		}

		public Car(int ownerID, int year, string make, string model)
		{
			OwnerID = ownerID;
			Year = year;
			Make = make;
			Model = model;
		}
	}

	public static class CarDataService
	{
		public static List<Car> GetCars()
		{
			return new List<Car>{new Car(1, year: 2002, make: "Hyundai", model: "Elantra"), new Car(2, year: 2003, make: "Hyundai", model: "Excel"), new Car(1, year: 2010, make: "Ford", model: "Escort"), };
		}
	}
}
;