Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. public static string GetProductsInRange(ProductShopContext context)
  2. {
  3. var result = context
  4. .Products
  5. .Where(p => p.Price >= 500 && p.Price <= 1000)
  6. .OrderBy(p => p.Price)
  7. .Take(10)
  8. .Select(p => new ExportProductsOverDto()
  9. {
  10. Name = p.Name,
  11. Price = p.Price.ToString("G29"),
  12. Buyer = $"{p.Buyer.FirstName} {p.Buyer.LastName}"
  13. })
  14. .ToArray();
  15.  
  16. for (int i = 0; i < result.Length; i++)
  17. {
  18. if (string.IsNullOrWhiteSpace(result[i].Buyer))
  19. {
  20. result[i].Buyer = null;
  21. }
  22. }
  23.  
  24. XmlSerializer serializer = new XmlSerializer(
  25. typeof(ExportProductsOverDto[]),
  26. new XmlRootAttribute("Products"));
  27.  
  28. var namespaces = new XmlSerializerNamespaces();
  29. namespaces.Add(string.Empty, string.Empty);
  30.  
  31. StringBuilder sb = new StringBuilder();
  32.  
  33. using (var writer = new StringWriter(sb))
  34. {
  35. serializer
  36. .Serialize(writer, result,namespaces);
  37. }
  38.  
  39. return sb.ToString().TrimEnd();
  40. }
  41.  
  42.  
  43. [XmlType("Product")]
  44. public class ExportProductsOverDto
  45. {
  46. [XmlElement("name")]
  47. public string Name { get; set; }
  48.  
  49. [XmlElement("price")]
  50. public string Price { get; set; }
  51.  
  52. [XmlElement("buyer")]
  53. public string Buyer { get; set; }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement