NelIfandieva

C#DBAdvanced_JsonProcessing_ProductShop_Ex06

Mar 28th, 2019
80
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static string GetSoldProducts(ProductShopContext context)
  2.         {
  3.             var soldProducts = context.Users
  4.                // .Where(x => x.ProductsSold.Any(a => a.BuyerId != null))
  5.                 .Select(u => new
  6.                 {
  7.                     firstName = u.FirstName,
  8.                     lastName = u.LastName,
  9.                     soldProducts = u.ProductsSold
  10.                     .Where(x => x.BuyerId != null)
  11.                     .Select(p => new
  12.                     {
  13.                         name = p.Name,
  14.                         price = p.Price,
  15.                         buyerFirstName = p.Buyer.FirstName,
  16.                         buyerLastName = p.Buyer.LastName
  17.                     })
  18.                 })
  19.                 .OrderBy(u => u.lastName)
  20.                 .ThenBy(u => u.firstName)
  21.                 .ToList();
  22.  
  23.             return JsonConvert.SerializeObject(soldProducts, Formatting.Indented);
  24.         }
RAW Paste Data