Hello, everyone!
I would like to add all cars, that were made by person into a new collection of new models.
I would like to add all cars, that were made by person into a new collection of new models.
C#:
using System;
using System.Collections.Generic;
using System.Linq;
namespace JoinListsIntoNewModel
{
public class Cars
{
public int Id { get; set; }
public int MadeByPersonId { get; set; }
public string MakerCompany { get; set; }
public int ModelOfYear { get; set; }
public string Color { get; set; }
}
public class MadeBy
{
public int Id { get; set; }
public string PersonName { get; set; }
public int YearOfProduction { get; set; }
//public virtual ICollection<Cars> PersonCars { get; set; }
}
public class UnitedFullModel : MadeBy
{
public List<Cars> CarsCollection { get; set; }
}
internal class Program
{
public static void Main()
{
var cars = new List<Cars>
{
new Cars { Id = 1, MadeByPersonId = 1, MakerCompany = "Honda", ModelOfYear = 2000, Color = "Black" },
new Cars { Id = 2, MadeByPersonId = 1, MakerCompany = "Suzuki", ModelOfYear = 1999, Color = "White" },
new Cars { Id = 3, MadeByPersonId = 1, MakerCompany = "Toyota", ModelOfYear = 1988, Color = "Green" },
new Cars { Id = 4, MadeByPersonId = 3, MakerCompany = "Kia", ModelOfYear = 2121, Color = "Blue" }
};
var madeBy = new List<MadeBy>
{
new MadeBy {Id = 1, PersonName = "Tim", YearOfProduction = 1998},
new MadeBy {Id = 3, PersonName = "John", YearOfProduction = 2021},
new MadeBy {Id = 4, PersonName = "Barbara", YearOfProduction = 1991 }
};
var fullElements = madeBy.Join(cars,
mad => mad.Id,
car => car.MadeByPersonId,
(mad, car) => new UnitedFullModel
{
Id = mad.Id,
PersonName = mad.PersonName,
YearOfProduction = mad.YearOfProduction,
//CarsCollection = cars.Where(x => x.MadeByPersonId == mad.Id)
}).ToList();
Console.ReadLine();
}
}
}