iliyazzSU

ExportCoachesWithTheirFootballers

Mar 31st, 2023
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 KB | None | 0 0
  1.         public static string ExportCoachesWithTheirFootballers(FootballersContext context)
  2.         {
  3.             XmlRootAttribute xmlRoot = new XmlRootAttribute("Coaches");
  4.             XmlSerializer xmlSerializer = new XmlSerializer(typeof(ExportCoachDto[]), xmlRoot);
  5.  
  6.             XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
  7.             namespaces.Add(string.Empty, string.Empty);
  8.  
  9.             StringBuilder sb = new StringBuilder();
  10.             using StringWriter writer = new StringWriter(sb);
  11.  
  12.             var coaches = context
  13.                 .Coaches
  14.                 .Where(c => c.Footballers.Any())
  15.                 .Select(c => new ExportCoachDto()
  16.                 {
  17.                     CoachName = c.Name,
  18.                     FootballersCount = c.Footballers.Count(),
  19.                     Footballers = c.Footballers
  20.                         .Select(f => new ExportCoachFootballerDto()
  21.                         {
  22.                             Name = f.Name,
  23.                             Position = f.PositionType.ToString()
  24.                         })
  25.                         .OrderBy(f => f.Name)
  26.                         .ToArray()
  27.                 })
  28.                 .OrderByDescending(c => c.FootballersCount)
  29.                 .ThenBy(f => f.CoachName)
  30.                 .ToArray();
  31.             xmlSerializer.Serialize(writer, coaches, namespaces);
  32.             return sb.ToString().TrimEnd();
  33.         }
Advertisement
Add Comment
Please, Sign In to add comment