//8
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            var users = context
                        .Users
                        .Where(u => u.ProductsSold.Any())
                        .Select(u => new UserWithProductSExportDto
            {
                FirstName    = u.FirstName,
                LastName     = u.LastName,
                Age          = u.Age,
                SoldProducts = new SoldProductsForUserExportDto
                {
                    Count    = u.ProductsSold.Count,
                    Products = u.ProductsSold
                               .Select(p => new ProductSoldDto
                    {
                        Name  = p.Name,
                        Price = p.Price
                    })
                               .OrderByDescending(p => p.Price)
                               .ToList()
                }
            })
                        .OrderByDescending(u => u.SoldProducts.Count)
                        .ToList();

            var allUsers = new AllUsersDto
            {
                Count = users.Count,
                Users = users.Take(10).ToList()
            };

            var attr       = new XmlRootAttribute("Users");
            var serializer = new XmlSerializer(typeof(AllUsersDto), attr);

            StringBuilder sb = new StringBuilder();

            var namespaces = new XmlSerializerNamespaces(new[]
            {
                XmlQualifiedName.Empty
            });

            serializer.Serialize(new StringWriter(sb), allUsers, namespaces);

            return(sb.ToString().TrimEnd());
        }
Exemplo n.º 2
0
        // 08. Users and Products
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            var users = new AllUsersDto
            {
                Count = context.Users.Where(u => u.ProductsSold.Count > 0).Count(),
                Users = context.Users
                        .Where(u => u.ProductsSold.Count > 0)
                        .ToArray()
                        .Select(u => new FullUserDto
                {
                    FirstName    = u.FirstName,
                    LastName     = u.LastName,
                    Age          = u.Age,
                    SoldProducts = new SoldProductsDto
                    {
                        Count    = u.ProductsSold.Count,
                        Products = u.ProductsSold.Select(ps => new UserProductDto
                        {
                            Name  = ps.Name,
                            Price = ps.Price
                        })
                                   .OrderByDescending(p => p.Price)
                                   .ToArray()
                    }
                })
                        .OrderByDescending(u => u.SoldProducts.Count)
                        .Take(10)
                        .ToArray()
            };

            XmlSerializer serializer = new XmlSerializer(typeof(AllUsersDto), new XmlRootAttribute("Users"));

            StringWriter writer = new StringWriter();

            XmlSerializerNamespaces emptyNamespace = new XmlSerializerNamespaces();

            emptyNamespace.Add("", "");

            serializer.Serialize(writer, users, emptyNamespace);

            return(writer.ToString());
        }