using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
public class Test
{
public class SAPItem
{
public string AddressLine1 { set; get; }
public string FiveDigitZip { set; get; }
public double? Latitude { set; get; }
public double? Longitude { set; get; }
public string MDM_Latitude { set; get; }
public string MDM_Longitude { set; get; }
}
public class MapPoint
{
public double Latitude { set; get; }
public double Longitude { set; get; }
}
static List<SAPItem> SAPItems = new List<SAPItem>
{
new SAPItem
{
AddressLine1 = "Address 1",
FiveDigitZip = "12345"
},
new SAPItem
{
AddressLine1 = "Address 2",
FiveDigitZip = "67890"
}
};
public static void Main(string[] args)
{
PullInfo();
}
public static void PullInfo()
{
// Create tasks to update items with latitude and longitude
var tasks
= SAPItems.Where(item => item.Latitude == null || item.Longitude == null)
.Select(item =>
GetMapPoint(item.AddressLine1 + " " + item.FiveDigitZip)
.ContinueWith(pointTask => {
item.MDM_Latitude = pointTask.Result.Latitude.ToString();
item.MDM_Longitude = pointTask.Result.Longitude.ToString();
}));
// Wait for tasks completion (it will block the current thread)
Task.WaitAll(tasks.ToArray());
// Iterate to append Lat and Long
foreach (var item in SAPItems)
Console.WriteLine(item.MDM_Latitude + " " + item.MDM_Longitude);
}
private static Task<MapPoint> GetMapPoint(string add)
{
return Task.FromResult(new MapPoint { Latitude = 1, Longitude = 1 });
}
}