Beispiel #1
0
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            var userProducts = context.Users
                               .Where(s => s.ProductsSold.Count >= 1)
                               .Select(x => new ExportUserWithAgeCountProductsDto
            {
                FirstName    = x.FirstName,
                LastName     = x.LastName,
                Age          = x.Age,
                SoldProducts = new ExportSoldProductsDto
                {
                    Count = x.ProductsSold.Count,

                    Products = x.ProductsSold.Select(s => new ExportSoldProductsProductDto
                    {
                        Name  = s.Name,
                        Price = s.Price
                    })
                               .OrderByDescending(t => t.Price)
                               .ToArray()
                }
            })
                               .OrderByDescending(q => q.SoldProducts.Count)
                               .Take(10)
                               .ToArray();

            var users = new ExportUsersDto
            {
                Count = context.Users.Count(s => s.ProductsSold.Count >= 1),
                Users = userProducts
            };

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

            var sb = new StringBuilder();

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

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

            return(sb.ToString().TrimEnd());
        }
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            var users = context.Users
                        .Where(u => u.ProductsSold.Count > 0)
                        .OrderByDescending(u => u.ProductsSold.Count)
                        .Select(x => new ExportUserWithProductDto
            {
                FirstName    = x.FirstName,
                LastName     = x.LastName,
                Age          = x.Age,
                SoldProducts = new ExportProductCountDto
                {
                    Count    = x.ProductsSold.Count,
                    Products = x.ProductsSold.Select(p => new ExportProductSimpleDto
                    {
                        Name  = p.Name,
                        Price = p.Price
                    }).OrderByDescending(p => p.Price)
                               .ToList()
                }
            })
                        .Take(10)
                        .ToList();

            var customExport = new ExportUsersDto
            {
                Count = context.Users.Where(u => u.ProductsSold.Count > 0).Count(),
                Users = users
            };

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

            var sb = new StringBuilder();

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

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

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