Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string GetUsersWithProducts(ProductShopContext context)
- {
- var users = new
- {
- usersCount = context.Users.Count(x => x.ProductsSold.Any(y => y.Buyer != null)),
- users = context.Users
- .Where(x=>x.ProductsSold.Any(y=>y.Buyer != null))
- .OrderByDescending(u => u.ProductsSold.Count(ps => ps.Buyer != null))
- .Select(y => new
- {
- firstName = y.FirstName,
- lastName = y.LastName,
- age = y.Age,
- soldProducts = new
- {
- count = y.ProductsSold.Count(b => b.Buyer != null),
- products = y.ProductsSold
- .Where(b => b.Buyer != null).Select(s => new
- {
- name = s.Name,
- price = s.Price
- }).ToArray()
- }
- }).ToArray()
- };
- var result = JsonConvert.SerializeObject(users, new JsonSerializerSettings()
- {
- Formatting = Formatting.Indented,
- NullValueHandling = NullValueHandling.Ignore
- });
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement