Ejemplo n.º 1
0
        //08.ExportUsersAndProducts
        //working solution form q2kPetrov
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            const string root = "Users";

            var users = context
                        .Users
                        .ToList()
                        .Where(u => u.ProductsSold.Any())
                        .OrderByDescending(u => u.ProductsSold.Count())
                        .Select(u => new ExportUserDto
            {
                FirstName    = u.FirstName,
                LastName     = u.LastName,
                Age          = u.Age,
                SoldProducts = new SoldProductsCountDto
                {
                    Count    = u.ProductsSold.Count(),
                    Products = u.ProductsSold
                               .Select(x => new ProductToDTO
                    {
                        Name  = x.Name,
                        Price = x.Price
                    })
                               .OrderByDescending(y => y.Price)
                               .ToList()
                }
            })
                        .Take(10)
                        .ToList();

            var userProductWithCount = new UserProductWithCount
            {
                Count = context.Users.Count(u => u.ProductsSold.Any()),
                Users = users
            };

            var result = XmlConverter.Serialize(userProductWithCount, root);

            return(result);
        }
Ejemplo n.º 2
0
        // ******* Task 8 - Export Users and Products *******
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            var usersProduct = context.Users
                               .ToList() // We need to materialize ALL users because Judge requires that "smart" move
                               .Where(u => u.ProductsSold.Any())
                               .OrderByDescending(p => p.ProductsSold.Count)
                               .Select(u => new ExportUserDto
            {
                FirstName    = u.FirstName,
                LastName     = u.LastName,
                Age          = u.Age,
                SoldProducts = new ProductsWithCountDTO
                {
                    Count    = u.ProductsSold.Count,
                    Products = u.ProductsSold
                               .Select(x => new ProductToDTO
                    {
                        Name  = x.Name,
                        Price = x.Price
                    })
                               .OrderByDescending(y => y.Price)
                               .ToList()
                }
            })
                               .Take(10)
                               .ToList();

            var userProductWithCount = new UserProductWithCount
            {
                Count = context.Users.Count(u => u.ProductsSold.Any()),
                Users = usersProduct
            };

            var userProductsExport = XMLConverter.Serialize(userProductWithCount, "Users");

            return(userProductsExport);
        }