SHARE
TWEET

Untitled

a guest Jun 18th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Agent
  2. {
  3.     public string AgentId { set; get; }
  4.     public double Longitude { set; get; }
  5.     public double Latitude { set; get; }
  6. }
  7.      
  8. public class AgentDistance : Agent, IComparable<AgentDistance>
  9. {
  10.  
  11.     public AgentDistance(Agent agent)
  12.     {
  13.         if(agent == null)
  14.         {
  15.             throw new Exception("Can't initalize agent with distance since agent is null");
  16.         }
  17.         this.AgentId = agent.AgentId;
  18.         this.Latitude = agent.Latitude;
  19.         this.Longitude = agent.Longitude;
  20.     }
  21.  
  22.     public double Distance { set; get; }
  23.  
  24.     public int CompareTo(AgentDistance other)
  25.     {
  26.         if(other == null)
  27.         {
  28.             return 1;
  29.         }
  30.         return Distance.CompareTo(other.Distance);
  31.     }
  32. }
  33.      
  34. var agents = db.GetAgents();
  35. if(agents == null || agents.Count == 0)
  36. {
  37.     Console.WriteLine("No Results");
  38. }
  39. List<AgentDistance> agentsWithDistance = new List<AgentDistance>();
  40. foreach(Agent agent in agents)
  41. {
  42.     double res = ws.GetDistanceMeters(agent.Latitude, agent.Longitude, customerLatitude, customerLongitude);
  43.     agentsWithDistance.Add(new AgentDistance(agent) { Distance = res });
  44. }
  45. agentsWithDistance.Sort();
  46. for(int i = 0; i < 5; i++)
  47. {
  48.     Console.WriteLine(string.Format("agent_id:  {0}  distance: {1} m", agentsWithDistance[i].AgentId, agentsWithDistance[i].Distance));
  49. }
  50.      
  51. var agents = db.GetAgents();
  52. if(agents == null || agents.Count == 0)
  53. {
  54.     Console.WriteLine("No Results");
  55. }
  56.      
  57. var agents = db.GetAgents();
  58. if(agents == null || agents.Count == 0)
  59. {
  60.     Console.WriteLine("No Results");
  61.     return;
  62. }
  63.  
  64. var nearestAgents = agents
  65.     .Select(x => new
  66.     {
  67.         x.AgentId,
  68.         x.Latitude, x.Longitude,
  69.         DistanceToCustomer = ws.GetDistanceMeters(x.Latitude, x.Longitude, customerLatitude, customerLongitude)
  70.     })
  71.     .OrderBy(x => x.DistanceToCustomer);
  72. foreach (var agent in nearestAgents.Take(5))
  73. {
  74.     Console.WriteLine($"agent_id:  {agent.AgentId}  distance: {agent.DistanceToCustomer} m");
  75. }
  76.      
  77. public class DistancedAgent : Agent
  78.   {
  79.     public DistancedAgent(Agent source, double distance)
  80.     {
  81.       AgentId = source.AgentId;
  82.       Latitude = source.Latitude;
  83.       Longitude = source.Longitude;
  84.       Distance = distance;
  85.     }
  86.  
  87.     public double Distance { get; }
  88.  
  89.     public override string ToString()
  90.     {
  91.       return $"{Latitude}, {Longitude} => {Distance}";
  92.     }
  93.   }
  94.      
  95. public static class Extensions
  96. {
  97.     public static DistancedAgent WithDistance(this Agent agent, double distance)
  98.     {
  99.       return new DistancedAgent(agent, distance);
  100.     }
  101.  
  102.     public static IEnumerable<DistancedAgent> WithDistance(this IEnumerable<Agent> agents, Func<Agent, double> getDistance)
  103.     {
  104.       return agents?.Where(a => a != null).Select(a => a.WithDistance(getDistance(a))) ?? new DistancedAgent[0];
  105.     }
  106.  
  107.     public static IEnumerable<DistancedAgent> WithDistance(this IEnumerable<Agent> agents, IDistanceProvider distanceProvider)
  108.     {
  109.       return agents?.Where(a => a != null).Select(a => a.WithDistance(distanceProvider.GetDistance(a))) ?? new DistancedAgent[0];
  110.     }
  111. }
  112.      
  113. public interface IDistanceProvider
  114.   {
  115.     double GetDistance(Agent agent);
  116.   }
  117.      
  118. var agents = db.GetAgents();
  119.  
  120.   Func<Agent, double> getDistance = a => ws.GetDistanceMeters(a.Latitude, a.Longitude, customerLatitude, customerLongitude);
  121.  
  122.   foreach (DistancedAgent dAgent in agents.WithDistance(getDistance).OrderBy(da => da.Distance).Take(5))
  123.   {
  124.     Console.WriteLine(dAgent);
  125.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top