Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string GetUsersWithProducts(ProductShopContext context)
- {
- var count = context.Users
- .Where(u => u.ProductsSold.Any(p => p.BuyerId != null))
- .Select(u => new
- {
- firstName = u.FirstName,
- lastName = u.LastName,
- age = u.Age,
- productsSold = u.ProductsSold
- .Select(p => new
- {
- name = p.Name,
- price = p.Price
- })
- })
- .Count();
- string countToString = JsonConvert.SerializeObject(count);
- var collection = context.Users
- .Where(u => u.ProductsSold.Any(p => p.BuyerId != null))
- .Select(u => new
- {
- firstName = u.FirstName,
- lastName = u.LastName,
- age = u.Age,
- soldProducts = u.ProductsSold.Count,
- products = u.ProductsSold
- .Select(p => new
- {
- name = p.Name,
- price = p.Price
- })
- })
- .ToList();
- var jsonCollection = JsonConvert.SerializeObject(collection, Formatting.Indented, new JsonSerializerSettings
- {
- NullValueHandling = NullValueHandling.Ignore
- });
- /*JsonConvert.SerializeObject(response, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings {
- NullValueHandling = NullValueHandling.Ignore
- });*/
- return $"{"{"}\n\"usersCount\":{countToString},\n\"users\":\n{jsonCollection}";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement