//8. Users and Products public static string GetUsersWithProducts(ProductShopContext context) { //var serializer = new XmlSerializer(typeof(ExportUsersWithProductsDto), new XmlRootAttribute("Users")); StringBuilder sb = new StringBuilder(); var objectToSerialize = new ExportUsersWithProductsDto(); //get the objects from db into dto format objectToSerialize.SingleUsers = context.Users .Where(u => u.ProductsSold.Any()) .OrderByDescending(u => u.ProductsSold.Count) .Take(10) .ProjectTo <AnotherSingleUser>() //.Select(u => new AnotherSingleUser() //{ // FirstName = u.FirstName, // LastName = u.LastName, // Age = u.Age, // SoldProduct = new SoldProductsType() // { // Count = u.ProductsSold.Count, // SoldProducts = u.ProductsSold.Select(p => new SingleProduct // { // Name = p.Name, // Price = p.Price // }) // .OrderByDescending(p=> p.Price) // .ToArray() // } //}) .ToArray(); objectToSerialize.Count = context.Users.Where(u => u.ProductsSold.Any()).Count(); //for removal of namespace var nameSpaces = new XmlSerializerNamespaces ( new[] { XmlQualifiedName.Empty } ); //and writer , who needs a sb, everyrthing is written in the sb! using (StringWriter writer = new StringWriter(sb)) { //serializer.Serialize(writer, objectToSerialize, nameSpaces); }; return(sb.ToString()); }
//08 public static string GetUsersWithProducts(ProductShopContext context) { StringBuilder outPutStringBuilder = new StringBuilder(); var usersWithSoldProducts = context.Users .Include(u => u.ProductsSold) .Where(u => u.ProductsSold.Any(p => p.BuyerId != null)) .ToArray(); var users = new ExportUsersWithProductsDto() { Count = usersWithSoldProducts.Length, Users = usersWithSoldProducts .Select(u => new ExportUserDto() { FirstName = u.FirstName, LastName = u.LastName, Age = u.Age, SoldProducts = new ExportSoldProductSecondDto() { Count = u.ProductsSold.Count, Products = u.ProductsSold.Select(ps => new ExportProductsDto() { Name = ps.Name, Price = ps.Price }) .OrderByDescending(p => p.Price) .ToArray() } }) .OrderByDescending(u => u.SoldProducts.Count) .Take(10) .ToArray() }; var xmlSerializer = new XmlSerializer(typeof(ExportUsersWithProductsDto[]) , new XmlRootAttribute("Users")); var namespaces = new XmlSerializerNamespaces(); namespaces.Add("", ""); using (var writer = new StringWriter(outPutStringBuilder)) { xmlSerializer.Serialize(writer, users, namespaces); } return(outPutStringBuilder.ToString().TrimEnd()); }
public static string GetUsersWithProducts(ProductShopContext context) { var users = context.Users .Where(u => u.ProductsSold.Any(p => p.Buyer != null)) .OrderByDescending(u => u.ProductsSold.Count(p => p.Buyer != null)) .Select(u => new UserDto() { FirstName = u.FirstName, LastName = u.LastName, Age = u.Age, SoldProducts = new SoldProductWithCountDto() { Count = u.ProductsSold.Count(ps => ps.Buyer != null), SoldProducts = u.ProductsSold .Where(ps => ps.Buyer != null) .Select(p => new SoldProductDto() { Name = p.Name, Price = p.Price }) .OrderByDescending(p => p.Price) .ToArray() } }) .Take(10) .ToArray(); var result = new ExportUsersWithProductsDto() { Count = context.Users.Count(u => u.ProductsSold.Any()), Users = users }; XmlSerializer serializer = new XmlSerializer(typeof(ExportUsersWithProductsDto), new XmlRootAttribute("Users")); var sb = new StringBuilder(); var namespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") }); serializer.Serialize(new StringWriter(sb), result, namespaces); return(sb.ToString()); }