Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Agent
- {
- public string AgentId { set; get; }
- public double Longitude { set; get; }
- public double Latitude { set; get; }
- }
- public class AgentDistance : Agent, IComparable<AgentDistance>
- {
- public AgentDistance(Agent agent)
- {
- if(agent == null)
- {
- throw new Exception("Can't initalize agent with distance since agent is null");
- }
- this.AgentId = agent.AgentId;
- this.Latitude = agent.Latitude;
- this.Longitude = agent.Longitude;
- }
- public double Distance { set; get; }
- public int CompareTo(AgentDistance other)
- {
- if(other == null)
- {
- return 1;
- }
- return Distance.CompareTo(other.Distance);
- }
- }
- var agents = db.GetAgents();
- if(agents == null || agents.Count == 0)
- {
- Console.WriteLine("No Results");
- }
- List<AgentDistance> agentsWithDistance = new List<AgentDistance>();
- foreach(Agent agent in agents)
- {
- double res = ws.GetDistanceMeters(agent.Latitude, agent.Longitude, customerLatitude, customerLongitude);
- agentsWithDistance.Add(new AgentDistance(agent) { Distance = res });
- }
- agentsWithDistance.Sort();
- for(int i = 0; i < 5; i++)
- {
- Console.WriteLine(string.Format("agent_id: {0} distance: {1} m", agentsWithDistance[i].AgentId, agentsWithDistance[i].Distance));
- }
- var agents = db.GetAgents();
- if(agents == null || agents.Count == 0)
- {
- Console.WriteLine("No Results");
- }
- var agents = db.GetAgents();
- if(agents == null || agents.Count == 0)
- {
- Console.WriteLine("No Results");
- return;
- }
- var nearestAgents = agents
- .Select(x => new
- {
- x.AgentId,
- x.Latitude, x.Longitude,
- DistanceToCustomer = ws.GetDistanceMeters(x.Latitude, x.Longitude, customerLatitude, customerLongitude)
- })
- .OrderBy(x => x.DistanceToCustomer);
- foreach (var agent in nearestAgents.Take(5))
- {
- Console.WriteLine($"agent_id: {agent.AgentId} distance: {agent.DistanceToCustomer} m");
- }
- public class DistancedAgent : Agent
- {
- public DistancedAgent(Agent source, double distance)
- {
- AgentId = source.AgentId;
- Latitude = source.Latitude;
- Longitude = source.Longitude;
- Distance = distance;
- }
- public double Distance { get; }
- public override string ToString()
- {
- return $"{Latitude}, {Longitude} => {Distance}";
- }
- }
- public static class Extensions
- {
- public static DistancedAgent WithDistance(this Agent agent, double distance)
- {
- return new DistancedAgent(agent, distance);
- }
- public static IEnumerable<DistancedAgent> WithDistance(this IEnumerable<Agent> agents, Func<Agent, double> getDistance)
- {
- return agents?.Where(a => a != null).Select(a => a.WithDistance(getDistance(a))) ?? new DistancedAgent[0];
- }
- public static IEnumerable<DistancedAgent> WithDistance(this IEnumerable<Agent> agents, IDistanceProvider distanceProvider)
- {
- return agents?.Where(a => a != null).Select(a => a.WithDistance(distanceProvider.GetDistance(a))) ?? new DistancedAgent[0];
- }
- }
- public interface IDistanceProvider
- {
- double GetDistance(Agent agent);
- }
- var agents = db.GetAgents();
- Func<Agent, double> getDistance = a => ws.GetDistanceMeters(a.Latitude, a.Longitude, customerLatitude, customerLongitude);
- foreach (DistancedAgent dAgent in agents.WithDistance(getDistance).OrderBy(da => da.Distance).Take(5))
- {
- Console.WriteLine(dAgent);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement