예제 #1
0
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            var users = context.Users
                        .ToArray()
                        .Where(u => u.ProductsSold.Any(p => p.BuyerId != null))
                        .OrderByDescending(u => u.ProductsSold.Count)
                        .Take(10)
                        .Select(u => new UserWithProductsDto
            {
                FirstName    = u.FirstName,
                LastName     = u.LastName,
                Age          = u.Age,
                SoldProducts = new SoldProductDto
                {
                    Count    = u.ProductsSold.Count(),
                    Products = u.ProductsSold.Select(x => new ProductDto
                    {
                        Name  = x.Name,
                        Price = x.Price
                    })
                               .OrderByDescending(x => x.Price)
                               .ToArray()
                }
            })
                        .ToArray();


            var usersAndProducts = new UserAndProductsDto
            {
                Count = context.Users.Count(x => x.ProductsSold.Any()),
                Users = users
            };


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

            XmlSerializerNamespaces noNameSpace = new XmlSerializerNamespaces();

            noNameSpace.Add("", "");

            string result = string.Empty;

            using (var writer = new StringWriter())
            {
                serializer.Serialize(writer, usersAndProducts, noNameSpace);
                result = writer.ToString();
            }
            return(result);
        }
예제 #2
0
        // Problem. 08
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            var users = context.Users
                        .Where(u => u.ProductsSold.Any())
                        .OrderByDescending(p => p.ProductsSold.Count())
                        .Select(u => new UserWithProductsDto
            {
                FirstName    = u.FirstName,
                LastName     = u.LastName,
                Age          = u.Age,
                SoldProducts = new SoldProductDto
                {
                    Count    = u.ProductsSold.Count(),
                    Products = u.ProductsSold
                               .Select(p => new ProductDto
                    {
                        Name  = p.Name,
                        Price = p.Price
                    })
                               .OrderByDescending(p => p.Price)
                               .ToArray()
                }
            })
                        .Take(10)
                        .ToArray();

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

            using var writer = new StringWriter();

            var ns = new XmlSerializerNamespaces();

            ns.Add("", "");

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

            serializer.Serialize(writer, userAndProducts, ns);

            var userAndProductsXml = writer.GetStringBuilder();

            return(userAndProductsXml.ToString().TrimEnd());
        }
예제 #3
0
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            var result = context.Users
                         .Where(u => u.ProductsSold.Any(y => y.Buyer != null))
                         .Select(x => new UserProductsDto()
            {
                FirstName    = x.FirstName,
                LastName     = x.LastName,
                Age          = x.Age,
                SoldProducts = new SoldProdDto()
                {
                    Count    = x.ProductsSold.Count(ps => ps.Buyer != null),
                    Products = x.ProductsSold
                               .Where(p => p.Buyer != null)
                               .Select(p => new ProdsDto()
                    {
                        Name  = p.Name,
                        Price = p.Price
                    })
                               .OrderByDescending(p => p.Price)
                               .ToArray()
                }
            })
                         .OrderByDescending(x => x.SoldProducts.Count)
                         .ToArray();

            var result1 = new UserAndProductsDto()
            {
                Count         = result.Length,
                UsersProducts = result
            };

            XmlSerializerNamespaces qm = new XmlSerializerNamespaces();

            qm.Add("", "");

            XmlSerializer serializer = new XmlSerializer(typeof(UserAndProductsDto), new XmlRootAttribute("Users"));
            StringBuilder sb         = new StringBuilder();

            serializer.Serialize(new StringWriter(sb), result1, qm);

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