コード例 #1
0
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            var allUsers = context.Users
                           .Where(x => x.ProductsSold.Any())
                           .Select(x => new { x.FirstName, x.LastName, x.Age, x.ProductsSold })
                           .OrderByDescending(x => x.ProductsSold.Count())
                           .ToArray();

            var sortedUsers = new UsersAndProductsDTO()
            {
                count = allUsers.Count(),
                users = allUsers.Take(10)
                        .Select(x => new UserDTO
                {
                    FirstName    = x.FirstName,
                    LastName     = x.LastName,
                    Age          = x.Age,
                    SoldProducts = new SoldProductsDTO
                    {
                        count    = x.ProductsSold.Count(),
                        Products = x.ProductsSold
                                   .Select(s => new ProductDTO
                        {
                            Name  = s.Name,
                            Price = s.Price
                        })
                                   .OrderByDescending(s => s.Price)
                                   .ToList()
                    }
                }).ToList()
            };

            var           builder      = new StringBuilder();
            XmlSerializer serializer   = new XmlSerializer(typeof(UsersAndProductsDTO), new XmlRootAttribute("Users"));
            var           stringwriter = new StringWriter(builder);

            using (stringwriter)
            {
                serializer.Serialize(stringwriter, sortedUsers, GetXmlNamespaces());
            }

            return(builder.ToString());
        }
コード例 #2
0
        //08. Export Users and Products
        public static string GetUsersWithProducts(ProductShopContext context)
        {
            var users = context.Users
                        .AsEnumerable()
                        .Where(u => u.ProductsSold.Any(b => b.BuyerId != null))
                        .OrderByDescending(p => p.ProductsSold.Count(ps => ps.BuyerId != null))
                        .Select(u => new UserDTO
            {
                FirstName    = u.FirstName,
                LastName     = u.LastName,
                Age          = u.Age,
                SoldProducts = new SoldProductDTO
                {
                    Count    = u.ProductsSold.Count(p => p.BuyerId != null),
                    Products = u.ProductsSold
                               .Where(p => p.BuyerId != null)
                               .Select(p => new ProductDTO
                    {
                        Name  = p.Name,
                        Price = p.Price
                    })
                }
            })
                        .ToList();

            var result = new UsersAndProductsDTO()
            {
                UsersCount = users.Count(),
                Users      = users
            };

            var json = JsonConvert.SerializeObject(result, Formatting.Indented,
                                                   new JsonSerializerSettings()
            {
                NullValueHandling = NullValueHandling.Ignore
            });

            return(json);
        }