将两个列表的不同数据合并为一个

时间:2019-05-07 10:01:29

标签: c#

因此,如果我得到3类:一类保存驾驶员的数据(他的姓名,车牌),第二类保存汽车的数据(商标,车牌,制造年份,里程),第三类保存(塔克西)诸如司机姓名,车牌,汽车制造年份等数据。

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;
    }
}

问题是我有两个清单。第一个列表保存驾驶员数据,第二个列表保存汽车数据。现在,我不知道什么是获取“汽车”和“驾驶员”类数据的一部分(名称,调节板,年份)然后合并并添加到

的最佳方法。
List<Taksi> Taks = new List<Taksi>();

有可能吗?

2 个答案:

答案 0 :(得分:2)

我建议您重新考虑您的类结构-驱动程序不是Taksi类型,因此不应从该类派生。 Taksi可以被认为是驾驶员和汽车的组合,因此Taksi不应从Taksi衍生汽车,而应从Car衍生(基本上,您将Taksi视为汽车的一种,而不是将Car视为汽车的一种)。类型的塔克西语。

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;
    }
}

然后您可以像这样合并您的汽车和驾驶员列表:

  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() ;

答案 1 :(得分:1)

尝试一下,顺便说一句,汽车课不需要那么多属性。

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
            };