Advertisement
Iv555

Untitled

Jul 19th, 2022
1,016
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 KB | None | 0 0
  1. public static string ImportCars(CarDealerContext context, string inputJson)
  2.         {
  3.             IEnumerable<CarInputModel> dtoCars = JsonConvert.DeserializeObject<IEnumerable<CarInputModel>>(inputJson);
  4.  
  5.             List<Car> cars = new List<Car>();
  6.  
  7.             foreach (CarInputModel dtoCar in dtoCars)
  8.             {
  9.                 Car newCar = new Car
  10.                 {
  11.                     Make = dtoCar.Make,
  12.                     Model = dtoCar.Model,
  13.                     TravelledDistance = dtoCar.TravelledDistance,
  14.                 };
  15.                 foreach (int partId in dtoCar.PartsId.Distinct())
  16.                 {
  17.                     newCar.PartCars.Add(new PartCar
  18.                     {
  19.                         PartId = partId
  20.                     });
  21.                 }
  22.  
  23.                 cars.Add(newCar);
  24.             }
  25.  
  26.             context.Cars.AddRange(cars);
  27.             context.SaveChanges();
  28.  
  29.             return $"Successfully imported {cars.Count()}.";
  30.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement