Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string GetProductsInRange(ProductShopContext context)
- {
- var result = context
- .Products
- .Where(p => p.Price >= 500 && p.Price <= 1000)
- .OrderBy(p => p.Price)
- .Take(10)
- .Select(p => new ExportProductsOverDto()
- {
- Name = p.Name,
- Price = p.Price.ToString("G29"),
- Buyer = $"{p.Buyer.FirstName} {p.Buyer.LastName}"
- })
- .ToArray();
- for (int i = 0; i < result.Length; i++)
- {
- if (string.IsNullOrWhiteSpace(result[i].Buyer))
- {
- result[i].Buyer = null;
- }
- }
- XmlSerializer serializer = new XmlSerializer(
- typeof(ExportProductsOverDto[]),
- new XmlRootAttribute("Products"));
- var namespaces = new XmlSerializerNamespaces();
- namespaces.Add(string.Empty, string.Empty);
- StringBuilder sb = new StringBuilder();
- using (var writer = new StringWriter(sb))
- {
- serializer
- .Serialize(writer, result,namespaces);
- }
- return sb.ToString().TrimEnd();
- }
- [XmlType("Product")]
- public class ExportProductsOverDto
- {
- [XmlElement("name")]
- public string Name { get; set; }
- [XmlElement("price")]
- public string Price { get; set; }
- [XmlElement("buyer")]
- public string Buyer { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement