Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void GetUsersAndProducts()
- {
- using (var db = new ProductsShopContext())
- {
- var users = db.Users
- .Where(u => u.ProductsSold.Any(b => b.BuyerId != null))
- .OrderByDescending(u => u.ProductsSold.Count())
- .ThenBy(u => u.LastName)
- .Select(u => new
- {
- firstName = u.FirstName,
- lastName = u.LastName,
- age = u.Age,
- soldProducts = new
- {
- count = u.ProductsSold.Count(),
- products = u.ProductsSold.Where(p => p.BuyerId != null)
- .Select(p => new
- {
- name = p.Name,
- price = p.Price
- })
- }
- }).ToArray();
- var usersToJson = new
- {
- usersCount = users.Count(),
- users
- };
- var jsonString = JsonConvert.SerializeObject(usersToJson, Formatting.Indented,
- new JsonSerializerSettings()
- {
- DefaultValueHandling = DefaultValueHandling.Ignore
- });
- File.WriteAllText("OutputFiles/UsersAndProducts.json", jsonString);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement