Exemple #1
0
        //08. Export Users and Products

        public static string GetUsersWithProducts(ProductShopContext context)
        {
            StringBuilder sb = new StringBuilder();

            var users =
                context
                .Users
                .Where(u => u.ProductsSold.Any())
                .Select(u => new ExportUsersDto
            {
                FirstName      = u.FirstName,
                LastName       = u.LastName,
                Age            = u.Age,
                SoldProductDto = new SoldProductDto
                {
                    Count      = u.ProductsSold.Count,
                    ProductDto = u.ProductsSold.Select(p => new ProductDto
                    {
                        Name  = p.Name,
                        Price = p.Price
                    })
                                 .OrderByDescending(p => p.Price)
                                 .ToArray()
                }
            })
                .ToArray()
                .OrderByDescending(u => u.SoldProductDto.Count)
                .Take(10)
                .ToArray();

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

            var xmlSerializer = new XmlSerializer(typeof(ExportCustomUserProductDto),
                                                  new XmlRootAttribute("Users"));

            var namespaces = new XmlSerializerNamespaces();

            namespaces.Add("", "");

            using (var writer = new StringWriter(sb))
            {
                xmlSerializer.Serialize(writer, export, namespaces);
            }

            return(sb.ToString().TrimEnd());
        }
Exemple #2
0
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            XmlSerializer xs = new XmlSerializer(typeof(ExportCustomUserProductDto), new XmlRootAttribute("Users"));

            var users = context
                        .Users
                        .Where(x => x.ProductsSold.Any())
                        .Select(x => new ExportUserAndProductDto
            {
                FirstName      = x.FirstName,
                LastName       = x.LastName,
                Age            = x.Age,
                SoldProductDto = new SoldProductDto
                {
                    Count       = x.ProductsSold.Count,
                    ProductDtos = x.ProductsSold.Select(p => new ProductDto()
                    {
                        Name  = p.Name,
                        Price = p.Price
                    })
                                  .OrderByDescending(p => p.Price)
                                  .ToArray()
                }
            })
                        .OrderByDescending(x => x.SoldProductDto.Count)
                        .Take(10)
                        .ToArray();

            var export = new ExportCustomUserProductDto
            {
                Count = context.Users.Count(x => x.ProductsSold.Any()),
                ExportUserAndProductDto = users
            };



            StringBuilder sb         = new StringBuilder();
            var           namespaces = new XmlSerializerNamespaces(new[]
            {
                new XmlQualifiedName("", "")
            });

            using (StringWriter writer = new StringWriter(sb))
            {
                xs.Serialize(writer, export, namespaces);
            }

            return(sb.ToString().TrimEnd());
        }
Exemple #3
0
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            /*Select all users who have at least 1 sold product. Order them by the number of sold products (from highest to lowest).
             * Select only their first and last name, age, count of sold products and for each product - name and price sorted by price (descending).*/
            var users = context
                        .Users
                        .Where(x => x.ProductsSold.Any())
                        .Select(x => new ExportUserAndProductDto
            {
                FirstName      = x.FirstName,
                LastName       = x.LastName,
                Age            = x.Age,
                SoldProductDto = new SoldProductDto
                {
                    Count       = x.ProductsSold.Count,
                    ProductDtos = x.ProductsSold.Select(p => new ProductDto()
                    {
                        Name  = p.Name,
                        Price = p.Price
                    })
                                  .OrderByDescending(p => p.Price)
                                  .ToArray()
                }
            })
                        .OrderByDescending(x => x.SoldProductDto.Count)
                        .Take(10)
                        .ToArray();

            var customExport = new ExportCustomUserProductDto
            {
                Count = context.Users.Count(x => x.ProductsSold.Any()),
                ExportUserAndProductDto = users
            };

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(ExportCustomUserProductDto), new XmlRootAttribute("Users"));

            var sb = new StringBuilder();

            var namespaces = new XmlSerializerNamespaces(new[]
            {
                new XmlQualifiedName("", ""),
            });

            xmlSerializer.Serialize(new StringWriter(sb), customExport, namespaces);


            return(sb.ToString().TrimEnd());
        }
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            var users = context.Users
                        .Where(u => u.ProductsSold.Any())
                        .Select(u => new ExportUserAndProductDto
            {
                FirstName      = u.FirstName,
                LastName       = u.LastName,
                Age            = u.Age,
                SoldProductDto = new SoldProductDto()
                {
                    Count    = u.ProductsSold.Count(ps => ps.Buyer != null),
                    Products = u.ProductsSold.Where(ps => ps.Buyer != null)
                               .Select(ps => new ProductDto
                    {
                        Name  = ps.Name,
                        Price = ps.Price
                    })
                               .OrderByDescending(x => x.Price)
                               .ToArray()
                }
            })
                        .OrderByDescending(x => x.SoldProductDto.Count)
                        .Take(10)
                        .ToArray();

            var customExport = new ExportCustomUserProductDto()
            {
                Count = context.Users
                        .Where(u => u.ProductsSold.Any()).Count(),
                ExportUserAndProductDtos = users
            };

            var sb = new StringBuilder();

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

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

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

            return(sb.ToString().TrimEnd());
        }
        //Task8
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            const string rootElement = "Users";

            ExportUsersAndProductsDto[] filteredUsers = context.Users
                                                        .Include(x => x.ProductsSold)
                                                        .ToArray()
                                                        .Where(u => u.ProductsSold.Any())
                                                        .Select(u => new ExportUsersAndProductsDto
            {
                FirstName    = u.FirstName,
                LastName     = u.LastName,
                Age          = u.Age,
                SoldProducts = new SoldProductDto
                {
                    Count    = u.ProductsSold.Count,
                    Products = u.ProductsSold
                               .Select(ps => new ProductDto
                    {
                        Name  = ps.Name,
                        Price = ps.Price,
                    })
                               .OrderByDescending(p => p.Price)
                               .ToArray()
                }
            })
                                                        .OrderByDescending(u => u.SoldProducts.Count)
                                                        .Take(10)
                                                        .ToArray();

            var customExport = new ExportCustomUserProductDto
            {
                Count = context.Users.Count(x => x.ProductsSold.Any()),
                Users = filteredUsers,
            };

            string result = XmlConverter.Serialize(customExport, rootElement);

            return(result);
        }