Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string GetCarsWithTheirListOfParts(CarDealerContext context)
- {
- var cars = context.Cars
- .Include(c => c.PartCars)
- .ThenInclude(c => c.Part)
- .Select(c => new
- {
- car = new
- {
- Make = c.Make,
- Model = c.Model,
- TravelledDistance = c.TravelledDistance
- },
- parts = c.PartCars
- .Select(p => new
- {
- Name = p.Part.Name,
- Price = $"{p.Part.Price:F2}"
- })
- .ToList()
- })
- .ToList();
- string json = JsonConvert.SerializeObject(cars, Formatting.Indented);
- return json;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement