Beispiel #1
0
        private static void GetUsersAndProducts(ProductShopContext context, IMapper mapper)
        {
            UserRootDto users = new UserRootDto()
            {
                UserCount   = context.Users.Count(u => u.ProductsSold.Count >= 1),
                UserDetails = context.Users
                              .Where(x => x.ProductsSold.Count >= 1)
                              .OrderByDescending(x => x.ProductsSold.Count)
                              .Select(x => new UserDetailsDto()
                {
                    FirstName    = x.FirstName,
                    LastName     = x.LastName,
                    Age          = x.Age,
                    SoldProducts = x.ProductsSold
                                   .Select(ps => new SoldProductsByUserDto()
                    {
                        Count        = x.ProductsSold.Count(),
                        ProductsList = x.ProductsSold
                                       .Select(p => new ProductsListDto()
                        {
                            Name  = p.Name,
                            Price = p.Price
                        }).ToArray()
                    }).ToArray()
                }).ToArray()
            };

            string jsonString = JsonConvert.SerializeObject(users, Formatting.Indented);

            File.WriteAllText("../../../Dto/Export/users-and-products.json", jsonString);
            Console.WriteLine();
        }
        //8. Users and Products
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            /*Select 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).
             * Take top 10 records.*/
            StringBuilder sb         = new StringBuilder();
            var           namespaces = new XmlSerializerNamespaces();

            namespaces.Add("", "");

            var users = new UserRootDto()
            {
                Count = context.Users.Count(u => u.ProductsSold.Any(p => p.Buyer != null)),
                Users = context.Users
                        .AsEnumerable() // or else inmemory error from judge
                        .Where(u => u.ProductsSold.Any(p => p.Buyer != null))
                        .OrderByDescending(u => u.ProductsSold.Count)
                        .Take(10)
                        .Select(u => new UserExportDto()
                {
                    FirstName    = u.FirstName,
                    LastName     = u.LastName,
                    Age          = u.Age,
                    SoldProducts = new SoldProductsDto()
                    {
                        Count    = u.ProductsSold.Count(ps => ps.Buyer != null),
                        Products = u.ProductsSold
                                   .Where(ps => ps.Buyer != null)
                                   .Select(ps => new ExportProductSoldDto()
                        {
                            Name  = ps.Name,
                            Price = ps.Price
                        })
                                   .OrderByDescending(p => p.Price)
                                   .ToList()
                    }
                })
                        .ToList()
            };

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

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

            return(sb.ToString().TrimEnd());
        }
Beispiel #3
0
        //08.Users and Products
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            const string root = "Users";

            var textWriter = new StringWriter();
            var nameSpace  = new XmlSerializerNamespaces();

            nameSpace.Add("", "");

            var users = new UserRootDto()
            {
                Count = context.Users.Count(u => u.ProductsSold.Any(p => p.Buyer != null)),
                Users = context
                        .Users
                        .ToArray()
                        .Where(u => u.ProductsSold.Count >= 1)
                        .OrderByDescending(x => x.ProductsSold.Count())
                        .Take(10)
                        .Select(u => new GetUsersWithProductsModel()
                {
                    FirstName    = u.FirstName,
                    LastName     = u.LastName,
                    Age          = u.Age,
                    SoldProducts = new SoldProductsModel()
                    {
                        Count    = u.ProductsSold.Count(ps => ps.Buyer != null),
                        Products = u.ProductsSold
                                   .ToArray()
                                   .Where(ps => ps.Buyer != null)
                                   .Select(ps => new ProductModel()
                        {
                            Name  = ps.Name,
                            Price = ps.Price
                        })
                                   .OrderByDescending(x => x.Price)
                                   .ToArray()
                    }
                })
                        .ToArray()
            };

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(UserRootDto), new XmlRootAttribute(root));

            xmlSerializer.Serialize(textWriter, users, nameSpace);

            return(textWriter.ToString());
        }
Beispiel #4
0
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            StringBuilder sb           = new StringBuilder();
            StringWriter  stringWriter = new StringWriter(sb);

            XmlRootAttribute        xmlRoot              = new XmlRootAttribute("Users");
            XmlSerializer           xmlSerializer        = new XmlSerializer(typeof(UserRootDto), xmlRoot);
            XmlSerializerNamespaces serializerNamespaces = new XmlSerializerNamespaces();

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

            var userWithProductsDtos = new UserRootDto
            {
                Count = context.Users.Where(u => u.ProductsSold.Count > 0).Count().ToString(),
                Users = context
                        .Users
                        .Where(u => u.ProductsSold.Count > 0)
                        .OrderByDescending(u => u.ProductsSold.Count)
                        .Take(10)
                        .Select(u => new ExportUserWithProductsDto
                {
                    FirstName    = u.FirstName,
                    LastName     = u.LastName,
                    Age          = u.Age.ToString(),
                    SoldProducts = new ExportSoldProductDto()
                    {
                        Count        = u.ProductsSold.Count.ToString(),
                        SoldProducts = u.ProductsSold
                                       .Where(p => p.Buyer != null)
                                       .OrderByDescending(ps => ps.Price)
                                       .Select(ps => new SoldProductDto
                        {
                            Name  = ps.Name,
                            Price = ps.Price.ToString()
                        })
                                       .ToArray()
                    }
                })
                        .ToArray()
            };

            xmlSerializer.Serialize(stringWriter, userWithProductsDtos, serializerNamespaces);

            return(sb.ToString().Trim());
        }
        private static void GetUsersAndProducts(ProductShopContext dbContext)
        {
            var users = new UserRootDto()
            {
                Count = dbContext.Users.Count(),
                Users = dbContext
                        .Users
                        .Where(u => u.ProductSold.Count >= 1)
                        .OrderByDescending(u => u.ProductSold.Count)
                        .Select(u => new UserExportDto()
                {
                    FirstName = u.FirstName,
                    LastName  = u.LastName,
                    Age       = u.Age.ToString(),
                    Products  = new ProductSoldRootDto()
                    {
                        Count           = u.ProductSold.Count,
                        ProductSoldDtos = u.ProductSold.Select(s => new ProductSoldDto()
                        {
                            Name  = s.Name,
                            Price = s.Price
                        })
                                          .ToArray()
                    }
                })
                        .ToArray()
            };

            StringBuilder           sb            = new StringBuilder();
            XmlSerializer           serializer    = new XmlSerializer(typeof(UserRootDto), new XmlRootAttribute("users"));
            XmlSerializerNamespaces xmlNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });

            serializer.Serialize(new StringWriter(sb), users, xmlNamespaces);

            File.WriteAllText("../../../Files/Export/users-and-products.xml", sb.ToString());
        }
Beispiel #6
0
        private static void GetUserAndProducts(ProductShopContext db)
        {
            var users = new UserRootDto
            {
                Count = db.Users.Count(),
                Users = db.Users
                        .Where(x => x.ProductsSold.Count >= 1)
                        .Select(x => new UserPdto
                {
                    FirstName    = x.FirstName,
                    LastName     = x.LastName,
                    Age          = x.Age.ToString(),
                    SoldProducts = new SoldProductDto
                    {
                        Count        = x.ProductsSold.Count(),
                        ProductUdtos = x.ProductsSold
                                       .Select(k => new ProductUdto
                        {
                            Name  = k.Name,
                            Price = k.Price
                        })
                                       .ToList()
                    }
                })
                        .ToList()
            };

            var sb            = new StringBuilder();
            var xmlNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });

            var serializer = new XmlSerializer(typeof(UserRootDto), new XmlRootAttribute("users"));

            serializer.Serialize(new StringWriter(sb), users, xmlNamespaces);

            File.WriteAllText("../../../Xml/users-and-products.xml", sb.ToString());
        }