Scr1pt25
Scr1pt25

Reputation: 57

Combining different data of two lists into one

So if i got 3 classes: one holds data of the Driver(his name, license plate),second which holds the cars data(trademark,license plate,year it was made,mileage),third should hold(Taksi) the data like drivers name,registration plate,year the car was made.

public class Car : Taksi
{
    public string Trademark { get; set; }
    public new int RegPlate { get; set; }
    public int YearMade { get; set; }
    public int Mileage { get; set; }
    public Car (string Trademark, int RegPlate, int YearMade, int Mileage)
    {
        this.Trademark = Trademark;
        this.RegPlate = RegPlate;
        this.YearMade = YearMade;
        this.Mileage = Mileage;
    }
}

public class Driver : Taksi
{
    public new string Name { get; set; }
    public new int RegPlate { get; set; }
    public Driver(string Name , int RegPlate )
    {
        this.Name = Name;
        this.RegPlate = RegPlate ;
    }
}

public class Taksi
{
    public string Name { get; set; }
    public int RegPlate { get; set; }
    public string Trademark { get; set; }
    public int YearMade { get; set; }
    public int Mileage { get; set; }
    public Taksi()
    {
    }
    public Taksi(string Name,string Trademark,int YearMade)
    {
        this.Name = Name;
        this.Trademark = Trademark;
        this.YearMade = YearMade;
    }
}

Problem is that i have two lists. First list holds data of driver and the second holds data of the car. Now i dont know what is the best way to to take parts of data(Name,regplate,yearmade) of the "Car" and "Driver"classes then have them combined and added to

List<Taksi> Taks = new List<Taksi>();

Is it possible?

Upvotes: 0

Views: 115

Answers (2)

BeiBei ZHU
BeiBei ZHU

Reputation: 363

try this, BTW, Car class no need so many property.

var cars = new List<Car>();
var drivers = new List<Driver>();

var taksi = from c in cars
            from d in drivers
            where c.RegPlate == d.RegPlate
            select new Taksi()
            {
                 Name = d.Name,
                 RegPlate = d.RegPlate,
                 Trademark = c.Trademark,
                 YearMade = c.YearMade,
                 Mileage = c.Mileage
            };

Upvotes: 1

PaulF
PaulF

Reputation: 6773

I would suggest that you reconsider your class structure - a Driver is not a type of Taksi so should not be derived from that class. A Taksi could be consider a combination of a Driver & a Car, so rather than derive a Car from a Taksi, a Taksi should be derived from a Car (basically you view a Taksi as a type of Car - rather than a Car as a type of Taksi).

public class Car
{
    public string Trademark { get; set; }
    public int YearMade { get; set; }
    public int Mileage { get; set; }
    public int RegPlate { get; set; }
    public Car (string Trademark, int RegPlate, int YearMade, int Mileage)
    {
        this.Trademark = Trademark;
        this.RegPlate = RegPlate;
        this.YearMade = YearMade;
        this.Mileage = Mileage;
    }
}

public class Driver
{
    public string Name { get; set; }
    public int RegPlate { get; set; }
    public Driver(string Name , int RegPlate )
    {
        this.Name = Name;
        this.RegPlate = RegPlate ;
    }
}

public class Taksi : Car
{
    public Driver TaksiDriver { get; set; }
    public Taksi(Driver TaksiDriver, Car TaksiCar)
      : base(TaksiCar.Trademark, TaksiCar.RegPlate, TaksiCar.YearMade, TaksiCar.Mileage)
    {
        this.TaksiDriver = TaksiDriver;
    }
}

You could then combine your list of Cars & Drivers like this :

  List<Car> cars = new List<Car>(); 
  List<Driver> drivers = new List<Driver>();
  .....
  .....

  List<Taksi> taksis = (from car in cars
    join driver in drivers on car.RegPlate equals driver.RegPlate
    select new Taksi( driver, car )).ToList() ;

Upvotes: 2

Related Questions