Exemple #1
0
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            var usersProducts = context
                                .Users
                                .Where(u => u.ProductsSold.Any())
                                .OrderByDescending(u => u.ProductsSold.Count)
                                .Select(u => new ExportUsersAndProductsDto
            {
                FirstName   = u.FirstName,
                LastName    = u.LastName,
                Age         = u.Age,
                SoldProduct = new SoldProductDto
                {
                    Count   = u.ProductsSold.Count,
                    Product = u.ProductsSold.Select(ps => new ProductDto
                    {
                        Name  = ps.Name,
                        Price = ps.Price
                    })
                              .OrderByDescending(ps => ps.Price)
                              .ToArray()
                }
            })
                                .Take(10)
                                .ToArray();

            var usersDto = new ExportUserDto
            {
                Count = context
                        .Users
                        .Count(u => u.ProductsSold.Any()),
                ExportUsersAndProductsDtos = usersProducts
            };

            var xml = new XmlSerializer(typeof(ExportUserDto), new XmlRootAttribute("Users"));

            var sb = new StringBuilder();

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

            xml.Serialize(new StringWriter(sb), usersDto, namespaces);

            return(sb.ToString().TrimEnd());
        }
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            var users = context.Users
                        .Where(u => u.ProductsSold.Count() > 0)
                        .Select(u => new UserDto
            {
                FirstName    = u.FirstName,
                LastName     = u.LastName,
                Age          = u.Age,
                SoldProducts = new SoldProductsDto
                {
                    Count    = u.ProductsSold.Where(pr => pr.Buyer != null).Count(),
                    Products = u.ProductsSold
                               .Where(pr => pr.Buyer != null)
                               .Select(p => new ProductWithNameAndPriceDto
                    {
                        Name  = p.Name,
                        Price = p.Price,
                    })
                               .OrderByDescending(x => x.Price)
                               .ToArray()
                }
            })
                        .OrderByDescending(x => x.SoldProducts.Count)
                        .ToArray();

            var resultObj = new ExportUserDto
            {
                Count = users.Count(),
                Users = users.Take(10).ToArray()
            };

            var serializer = new XmlSerializer(typeof(ExportUserDto));

            var sb = new StringBuilder();

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

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

            return(sb.ToString().TrimEnd());
        }
Exemple #3
0
        //Problem 08. Users and Products
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            StringBuilder sb = new StringBuilder();

            using StringWriter stringWriter = new StringWriter(sb);

            XmlSerializer           xmlSerializer = new XmlSerializer(typeof(ExportUserDto), new XmlRootAttribute("Users"));
            XmlSerializerNamespaces namespaces    = new XmlSerializerNamespaces();

            namespaces.Add(string.Empty, string.Empty);

            var users = new ExportUserDto()
            {
                Count = context.Users.Count(u => u.ProductsSold.Any(b => b.Buyer != null)),
                Users = context.Users.ToArray()
                        .Where(u => u.ProductsSold.Any(b => b.Buyer != null))
                        .OrderByDescending(u => u.ProductsSold.Count)
                        .Take(10)
                        .Select(u => new UserSoldProductDto()
                {
                    FirstName    = u.FirstName,
                    LastName     = u.LastName,
                    Age          = u.Age,
                    SoldProducts = new SoldProductDto()
                    {
                        Count    = u.ProductsSold.Count,
                        Products = u.ProductsSold.ToArray()
                                   .Where(p => p.Buyer != null)
                                   .Select(p => new ExportSoldProductDto()
                        {
                            Name  = p.Name,
                            Price = p.Price
                        })
                                   .OrderByDescending(p => p.Price)
                                   .ToArray()
                    }
                }).ToArray()
            };

            xmlSerializer.Serialize(stringWriter, users, namespaces);

            return(sb.ToString().TrimEnd());
        }