fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. public class Test
  7. {
  8. public static void Main()
  9. {
  10. var myData = new []
  11. {
  12. new { Name = "Bill", Action="aaa", Id = "832758" },
  13. new { Name = "Tony", Action="aaa", Id = "82fd58" },
  14. new { Name = "Bill", Action="bbb", Id = "532758" },
  15. new { Name = "Tony", Action="bbb", Id = "42fd58" }
  16. };
  17.  
  18. // group all the Names together
  19. var result = from m in myData
  20. group m by m.Name into names
  21. orderby names.Key
  22. select names;
  23.  
  24. // go through each Name and combine the underlying Actions and Ids
  25. var sbLines = new StringBuilder();
  26. foreach (var name in result)
  27. {
  28. var sb = new StringBuilder();
  29. sb.AppendFormat("Name: {0}, ", name.Key);
  30.  
  31. int count = 1;
  32. foreach (var item in name)
  33. {
  34. if(count > 1)
  35. sb.AppendFormat("Action_{0}: {1}, ", count, item.Action);
  36. else
  37. sb.AppendFormat("Action: {0}, ", item.Action);
  38. count++;
  39. }
  40.  
  41. count = 1;
  42. foreach (var item in name)
  43. {
  44. if(count > 1)
  45. sb.AppendFormat("Id_{0}: {1}, ", count, item.Id);
  46. else
  47. sb.AppendFormat("Id: {0}, ", item.Id);
  48. count++;
  49. }
  50.  
  51. sbLines.Append(sb.ToString().Trim(new char[] { ' ',',' }));
  52. sbLines.Append(Environment.NewLine);
  53. }
  54. Console.WriteLine(sbLines.ToString());
  55. }
  56. }
Success #stdin #stdout 0.05s 37320KB
stdin
Standard input is empty
stdout
Name: Bill, Action: aaa, Action_2: bbb, Id: 832758, Id_2: 532758
Name: Tony, Action: aaa, Action_2: bbb, Id: 82fd58, Id_2: 42fd58