fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Province
  6. {
  7. public string province { set; get; }
  8. public Nullable<int> provinceCode { set; get; }
  9. }
  10.  
  11. public class Zone
  12. {
  13. public string CityCode { get; set; }
  14. public string City { get; set; }
  15. public string Province { get; set; }
  16. public Nullable<int> ProvinceCode { get; set; }
  17. }
  18.  
  19. public class ZoneTest
  20. {
  21. public static void Main()
  22. {
  23. List<Zone> ZoneList = new List<Zone>();
  24.  
  25. Zone newZone = new Zone()
  26. {
  27. CityCode = "citycode1",
  28. City = "city1",
  29. Province = "province1",
  30. ProvinceCode = 1
  31. };
  32.  
  33. ZoneList.Add(newZone);
  34.  
  35. newZone = new Zone()
  36. {
  37. CityCode = "citycode2",
  38. City = "city2",
  39. Province = "province2",
  40. ProvinceCode = 2
  41. };
  42.  
  43. ZoneList.Add(newZone);
  44.  
  45. newZone = new Zone()
  46. {
  47. CityCode = "citycode2",
  48. City = "city2",
  49. Province = "province2",
  50. ProvinceCode = 2
  51. };
  52.  
  53. ZoneList.Add(newZone);
  54.  
  55. Console.WriteLine(ZoneList[0].ProvinceCode);
  56. Console.WriteLine(ZoneList[1].ProvinceCode);
  57. Console.WriteLine(ZoneList[2].ProvinceCode);
  58.  
  59. List<Province> ListZoneWithDistinct = ZoneList.GroupBy(x => new {x.Province, x.ProvinceCode}).Select(grp => new Province() {province = grp.First().Province, provinceCode = grp.First().ProvinceCode}).ToList();
  60.  
  61. Console.WriteLine("Count after Distinct: " + ListZoneWithDistinct.Count);
  62. Console.WriteLine(ListZoneWithDistinct[0].provinceCode);
  63. Console.WriteLine(ListZoneWithDistinct[1].provinceCode);
  64. }
  65. }
Success #stdin #stdout 0.05s 34096KB
stdin
Standard input is empty
stdout
1
2
2
Count after Distinct: 2
1
2