public static void GetUsersAndProducts(ShopDbContext db, IMapper mapper) { var user = new UserExportDto { Count = db.Users.Count(), Users = db.Users.Where(x => x.ProductsSold.Count >= 1) .Select(x => new UsersAndProductsDto() { FirstName = x.FirstName, LastName = x.LastName, Age = x.Age.ToString(), SoldProducts = new ProductSoldRootDto() { Count = x.ProductsSold.Count, ProductSoldDtos = x.ProductsSold.Select(s => new ProductsSoldDto() { Name = s.Name, Price = s.Price }).ToArray() } }).ToArray() }; StringBuilder sb = new StringBuilder(); XmlSerializer serializer = new XmlSerializer(typeof(UserExportDto), new XmlRootAttribute($"users")); serializer.Serialize(new StringWriter(sb), user); File.WriteAllText("users-and-products.xml", sb.ToString()); Console.WriteLine(); }
public static string GetUsersWithProducts(ProductShopContext context) { StringBuilder sb = new StringBuilder(); XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(); namespaces.Add(String.Empty, String.Empty); var users = new UserExportDto() { Count = context.Users.Count(u => u.ProductsSold.Any(p => p.Buyer != null)), Users = context.Users .ToArray() .Where(u => u.ProductsSold.Any(p => p.Buyer != null)) .OrderByDescending(u => u.ProductsSold.Count) .Take(10) .Select(u => new GetUsersWithproductsExportDto() { FirstName = u.FirstName, LastName = u.LastName, Age = u.Age, SoldProducts = new ProductsExportDto() { Count = u.ProductsSold.Count(ps => ps.Buyer != null), Products = u.ProductsSold .ToArray() .Where(ps => ps.Buyer != null) .Select(ps => new SoldProductsExportDto() { Name = ps.Name, Price = ps.Price }) .OrderByDescending(p => p.Price) .ToArray() } }).ToArray() }; XmlSerializer xmlSerializer = new XmlSerializer(typeof(UserExportDto), new XmlRootAttribute("Users")); xmlSerializer.Serialize(new StringWriter(sb), users, namespaces); return(sb.ToString().Trim()); }
public static string GetUsersWithProducts(ProductShopContext context) { const string root = "Users"; var serializer = new UserExportDto() { CountOfUsers = context.Users.Count(x => x.FirstName != null), Users = context.Users .Where(x => x.ProductsSold.Count > 0) .OrderByDescending(x => x.ProductsSold.Count) .Select(x => new NestedUsersExportDto() { FirstName = x.FirstName, LastName = x.LastName, Age = x.Age, SoldProducts = new ProductSoldDto() { CountOfProductsSoldByUser = x.ProductsSold.Count(p => p.Buyer != null), ProductsSoldByUser = x.ProductsSold .Where(p => p.Buyer != null) .Select(p => new NestedProductsSold() { Name = p.Name, Price = p.Price, }) .OrderByDescending(p => p.Name) .ThenByDescending(p => p.Price) //.Take(10) .ToList() .ToArray() } }) //.Take(10) .ToList() .ToArray() }; var result = XmlSerializer1 <UserExportDto>(serializer, root); return(result); }
public static string UsersAndProducts(ProductShoContext context) { var usersQuery = context.Users.Where(uc => uc.Sold.Count > 0); var users = new UserExportDto { Count = usersQuery.Count(), Users = usersQuery .Select(uc => new UserAndProductsDto { FirstName = uc.FirstName, LastName = uc.LastName, Age = uc.Age, SoldProducts = new SoldProductsExportDto { Count = uc.Sold.Count, Products = uc.Sold .Select(sp => new ProductByUserExportDto { Name = sp.Name, Price = sp.Price }) .ToArray() } }) .ToArray(), }; var serializer = new XmlSerializer(typeof(UserExportDto)); var namespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") }); var sb = new StringBuilder(); serializer.Serialize(new StringWriter(sb), users, namespaces); var result = sb.ToString(); return(result); }