Advertisement
dimitrix85

Untitled

Mar 26th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1.         public static string GetUsersWithProducts(ProductShopContext context)
  2.         {
  3.             var users = new
  4.             {
  5.                 usersCount = context.Users.Count(x => x.ProductsSold.Any(y => y.Buyer != null)),
  6.                 users = context.Users
  7.                 .Where(x=>x.ProductsSold.Any(y=>y.Buyer != null))
  8.                 .OrderByDescending(u => u.ProductsSold.Count(ps => ps.Buyer != null))
  9.                 .Select(y => new
  10.                 {
  11.                     firstName = y.FirstName,
  12.                     lastName = y.LastName,
  13.                     age = y.Age,
  14.                     soldProducts = new
  15.                     {
  16.                         count = y.ProductsSold.Count(b => b.Buyer != null),
  17.                         products = y.ProductsSold
  18.                         .Where(b => b.Buyer != null).Select(s => new
  19.                         {
  20.                             name = s.Name,
  21.                             price = s.Price
  22.                         }).ToArray()
  23.                     }
  24.                 }).ToArray()
  25.             };
  26.  
  27.  
  28.             var result = JsonConvert.SerializeObject(users, new JsonSerializerSettings()
  29.             {
  30.                 Formatting = Formatting.Indented,
  31.                 NullValueHandling = NullValueHandling.Ignore
  32.             });
  33.  
  34.             return result;
  35.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement