fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5.  
  6. public class Test
  7. {
  8. public class SAPItem
  9. {
  10. public string AddressLine1 { set; get; }
  11. public string FiveDigitZip { set; get; }
  12. public double? Latitude { set; get; }
  13. public double? Longitude { set; get; }
  14. public string MDM_Latitude { set; get; }
  15. public string MDM_Longitude { set; get; }
  16. }
  17.  
  18. public class MapPoint
  19. {
  20. public double Latitude { set; get; }
  21. public double Longitude { set; get; }
  22. }
  23.  
  24. static List<SAPItem> SAPItems = new List<SAPItem>
  25. {
  26. new SAPItem
  27. {
  28. AddressLine1 = "Address 1",
  29. FiveDigitZip = "12345"
  30. },
  31.  
  32. new SAPItem
  33. {
  34. AddressLine1 = "Address 2",
  35. FiveDigitZip = "67890"
  36. }
  37. };
  38.  
  39. public static void Main(string[] args)
  40. {
  41. PullInfo();
  42. }
  43.  
  44. public static void PullInfo()
  45. {
  46. // Create tasks to update items with latitude and longitude
  47. var tasks
  48. = SAPItems.Where(item => item.Latitude == null || item.Longitude == null)
  49. .Select(item =>
  50. GetMapPoint(item.AddressLine1 + " " + item.FiveDigitZip)
  51. .ContinueWith(pointTask => {
  52. item.MDM_Latitude = pointTask.Result.Latitude.ToString();
  53. item.MDM_Longitude = pointTask.Result.Longitude.ToString();
  54. }));
  55.  
  56. // Wait for tasks completion (it will block the current thread)
  57. Task.WaitAll(tasks.ToArray());
  58.  
  59. // Iterate to append Lat and Long
  60. foreach (var item in SAPItems)
  61. Console.WriteLine(item.MDM_Latitude + " " + item.MDM_Longitude);
  62. }
  63.  
  64. private static Task<MapPoint> GetMapPoint(string add)
  65. {
  66. return Task.FromResult(new MapPoint { Latitude = 1, Longitude = 1 });
  67. }
  68. }
Success #stdin #stdout 0.03s 265920KB
stdin
Standard input is empty
stdout
1 1
1 1