fork download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. Action[] actions =
  10. {
  11. new Action(1, new DateTime(2013,3,22), 4, 8),
  12. new Action(2, new DateTime(2013,3,31), 1, 8),
  13. new Action(3, new DateTime(2013,4,12), 3, 8),
  14. new Action(4, new DateTime(2013,3,22), 4, 9),
  15. new Action(5, new DateTime(2013,3,31), 1, 9),
  16. new Action(6, new DateTime(2013,4,12), 5, 9)
  17. };
  18.  
  19. PrintResult(GetActions(actions, 4, 1));
  20. PrintResult(GetActions(actions, 4, 5));
  21. PrintResult(GetActions(actions, 3, 5));
  22. }
  23.  
  24. public static void PrintResult(IEnumerable<int> result)
  25. {
  26. foreach(var item in result)
  27. Console.WriteLine(item);
  28. Console.WriteLine("---");
  29. }
  30.  
  31. public static IEnumerable<int> GetActions(IEnumerable<Action> actions, params int[] actionTypes)
  32. {
  33. return actions.GroupBy(x => x.UserID)
  34. .Select(g => g.OrderByDescending(x => x.ActionTime)
  35. .First())
  36. .Where(x => actionTypes.Contains(x.ActionType))
  37. .Select(x => x.ActionID);
  38. }
  39.  
  40. public class Action
  41. {
  42. public Action(int actionID, DateTime actionTime, int actionType, int userID)
  43. {
  44. ActionID = actionID;
  45. ActionTime = actionTime;
  46. ActionType = actionType;
  47. UserID = userID;
  48. }
  49.  
  50. public int ActionID { get; set; }
  51. public DateTime ActionTime { get; set; }
  52. public int ActionType { get; set; }
  53. public int UserID { get; set; }
  54. }
  55. }
Success #stdin #stdout 0.07s 34168KB
stdin
Standard input is empty
stdout
---
6
---
3
6
---