private static void UsersWithProducts(ProductShopContext context, XmlSerializerNamespaces xmlNamespaces) { var users = context.Users.Where(x => x.ProductsBought.Count >= 1) .Select(x => new UP_UserDto { FirstName = x.FirstName, LastName = x.LastName, Age = x.Age.ToString(), SoldProducts = new UP_SoldProductDto { Count = x.ProductsBought.Count, Product = x.ProductsBought.Select(p => new UP_ProductDto { Name = p.Name, Price = p.Price }).ToArray() } }) .OrderByDescending(x => x.SoldProducts.Count).ToArray(); var usersProducts = new UP_UsersDto { Count = users.Count(), Users = users }; XmlSerializer serializer = new XmlSerializer(typeof(UP_UsersDto)); using (var writer = new StreamWriter(@"..\..\..\..\Xml\users-and-products.xml")) { serializer.Serialize(writer, usersProducts, xmlNamespaces); } }
private static void UsersWithProducts(ProductShopContext context, XmlSerializerNamespaces xmlNamespaces) { var users = context.Users.Where(x => x.ProductsSold.Any(a => a.BuyerId != null)) .Select(x => new UP_UserDto { FirstName = x.FirstName, LastName = x.LastName, Age = x.Age.ToString(), SoldProducts = new UP_SoldProductDto { Count = x.ProductsSold.Where(b => b.BuyerId != null).Count(), Product = x.ProductsSold.Where(b => b.BuyerId != null) .Select(a => new UP_ProductDto { Name = a.Name, Price = a.Price }).ToArray() } }) .OrderByDescending(x => x.SoldProducts.Count) .ToArray(); var usersProducts = new UP_UsersDto { Count = users.Count(), Users = users }; XmlSerializer serializer = new XmlSerializer(typeof(UP_UsersDto)); using (var writer = new StreamWriter("users-and-products.xml")) { serializer.Serialize(writer, usersProducts, xmlNamespaces); } }