Guest User

Untitled

a guest
Jan 16th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. public string GetData(string userName, DateTime fromDate, DateTime toDate)
  2. {
  3. var userElement = xDox.Descendants("User")
  4. .SingleOrDefault(u => u.Element("Name").Value == userName);
  5.  
  6. var builder = new StringBuilder();
  7.  
  8. if (userElement != null)
  9. {
  10. var result = userElement.Descendants("Attempts")
  11. .Select(a => new
  12. {
  13. Place = a.Element("Place").Value,
  14. Date = DateTime.Parse(a.Element("Date").Value),
  15. Distance = int.Parse(a.Element("Distance").Value)
  16. })
  17.  
  18. .Where(a => a.Date >= fromDate
  19. && a.Date <= toDate)
  20.  
  21. .GroupBy(a => a.Place)
  22. .Select(g => new {Place = g.Key, Avg = g.Average(x => x.Distance)});
  23.  
  24. builder.AppendFormat("User:{0}", userName);
  25. builder.AppendLine();
  26.  
  27. builder.AppendFormat("Date:{0} to {1}", fromDate, toDate);
  28. builder.AppendLine();
  29.  
  30. foreach (var item in result)
  31. {
  32. builder.AppendFormat("Average Distance in {0}: {1}",
  33. item.Place, item .Avg);
  34. builder.AppendLine();
  35. }
  36. }
  37.  
  38. return builder.ToString();
  39. }
Add Comment
Please, Sign In to add comment