//08 public static string GetUsersWithProducts(ProductShopContext context) { const string rootElement = "Users"; var users = context.Users.ToArray() //in memory exeption in judge if we dont have toarray .Where(x => x.ProductsBought.Any()) .Select(x => new ExportUsersWithProductsDTO { FirstName = x.FirstName, LastName = x.LastName, Age = x.Age, SoldProducts = new SoldProductsDTO { Count = x.ProductsSold.Count, Products = x.ProductsSold.Select(y => new ProductDTO { Name = y.Name, Price = y.Price, }).OrderByDescending(p => p.Price) .ToArray(), } }).OrderByDescending(x => x.SoldProducts.Count) .Take(10) .ToArray(); var result = new ExportUserCountDTO { Count = context.Users.Where(x => x.ProductsSold.Any()).Count(), Users = users, }; var productsXml = XmlConverter.Serialize(result, rootElement); return(productsXml); }
//Problem 08 public static string GetUsersWithProducts(ProductShopContext context) { var usersAndProducts = context.Users .Where(p => p.ProductsSold.Any()) .Select(u => new ExportUserDTO { FirstName = u.FirstName, LastName = u.LastName, Age = u.Age, SoldProduct = new ExportProductCountDTO { Count = u.ProductsSold.Count, Products = u.ProductsSold.Select(p => new ExportProductDTO { Name = p.Name, Price = p.Price }) .OrderByDescending(p => p.Price) .ToArray() } }) .OrderByDescending(x => x.SoldProduct.Count) .Take(10) .ToArray(); var resultDto = new ExportUserCountDTO { Count = context.Users.Count(p => p.ProductsSold.Any()), Users = usersAndProducts }; var result = XmlConverter.Serialize(resultDto, "Users"); return(result); }
//Problem - 08 public static string GetUsersWithProducts(ProductShopContext context) { const string rootElement = "Users"; var users = context .Users .AsEnumerable() .Where(u => u.ProductsSold.Any(p => p.Buyer != null)) .Select(u => new ExportUserDTO { FirstName = u.FirstName, LastName = u.LastName, Age = u.Age, SoldProducts = new ExportProductCountDTO { Count = u.ProductsSold.Count(p => p.Buyer != null), Products = u.ProductsSold .Where(p => p.Buyer != null) .Select(p => new ExportProductInfoDTO { Name = p.Name, Price = p.Price }) .OrderByDescending(p => p.Price) .ToArray() } }) .OrderByDescending(x => x.SoldProducts.Count) .Take(10) .ToArray(); var xmlResult = new ExportUserCountDTO { Count = context.Users.Count(x => x.ProductsSold.Any()), Users = users }; string xml = XmlConverter.Serialize(xmlResult, rootElement); return(xml); }
public static string GetUsersWithProducts(ProductShopContext context) { var users = context .Users .Where(u => u.ProductsSold.Any()) .Select(u => new ExportUserDTO { FirstName = u.FirstName, LastName = u.LastName, Age = u.Age, SoldProducts = new ExportSoldProductsDTO { Count = u.ProductsSold.Count, Products = u.ProductsSold .Select(p => new ExportProductDetailsDTO { Name = p.Name, Price = p.Price }) .OrderByDescending(x => x.Price) .ToList() } }) .OrderByDescending(x => x.SoldProducts.Count) .Take(10) .ToList(); var finalUser = new ExportUserCountDTO { Count = context.Users.Count(u => u.ProductsSold.Any()), Users = users }; var root = "Users"; var result = XMLConverter.Serialize(finalUser, root); return(result); }