Finding the nearest agents to a customer using IComparableIComparable comparisionWebAPI - Return models vs entity and partial class with meta dataExceptions or something else?Finding nearest city from a center place (latitude and longitude) with radius in milesIComparable implementation for a class representing a versionRectangle ClassGet nearest driver from 2.5 millions of data using mongodbPANDAS nearest site algorithmNearest Neighbor Algorithm (general nearest, forward-nearest, or backward-nearest)Finding the nearest defibrillator exercise from CodingGames

Prime Sieve and brute force

Impedance ratio vs. SWR

How to tell your grandparent to not come to fetch you with their car?

What's up with this leaf?

Motivation - or how can I get myself to do the work I know I need to?

Pre-1972 sci-fi short story or novel: alien(?) tunnel where people try new moves and get destroyed if they're not the correct ones

What makes Ada the language of choice for the ISS's safety-critical systems?

How can I get an unreasonable manager to approve time off?

Is the term 'open source' a trademark?

How can "научись" mean "take it and keep trying"?

What is the `some` keyword in SwiftUI?

Why didn't Voldemort recognize that Dumbledore was affected by his curse?

A curious prime counting approximation or just data overfitting?

Logarithm of exponential

Passing multiple files through stdin (over ssh)

How come the nude protesters were not arrested?

Share calendar details request from manager's manager

Programming bare microcontroller chips

Is it legal for a bar bouncer to conficaste a fake ID

What do abbreviations in movie scripts stand for?

Someone whose aspirations exceed abilities or means

What can I, as a user, do about offensive reviews in App Store?

How to return a security deposit to a tenant

Arriving at the same result with the opposite hypotheses



Finding the nearest agents to a customer using IComparable


IComparable comparisionWebAPI - Return models vs entity and partial class with meta dataExceptions or something else?Finding nearest city from a center place (latitude and longitude) with radius in milesIComparable implementation for a class representing a versionRectangle ClassGet nearest driver from 2.5 millions of data using mongodbPANDAS nearest site algorithmNearest Neighbor Algorithm (general nearest, forward-nearest, or backward-nearest)Finding the nearest defibrillator exercise from CodingGames






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








6












$begingroup$


I got a C# class:



public class Agent 

public string AgentId set; get;
public double Longitude set; get;
public double Latitude set; get;



There are several agents in several different locations. During the program's lifetime, they can receive a call to another (latitude, longitude) point which is unknown during runtime. I got an API to calculate the distance in meters between their location and the given point. What I need eventually is to find the 5 closest agents to the given point.



What I did in order to achieve this is adding another class:



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);




And the usage:



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));



It works, but is there a more elegant way to do it? I'm not sure if adding another class might be a bit redundant since all it does is just adding a property for sorting, but adding the distance property to the Agent class, doesn't make so much sense.










share|improve this question











$endgroup$











  • $begingroup$
    @KonradRudolph what do you mean?
    $endgroup$
    – Yonatan Nir
    May 22 at 11:01










  • $begingroup$
    Never mind, my mistake.
    $endgroup$
    – Konrad Rudolph
    May 22 at 12:58

















6












$begingroup$


I got a C# class:



public class Agent 

public string AgentId set; get;
public double Longitude set; get;
public double Latitude set; get;



There are several agents in several different locations. During the program's lifetime, they can receive a call to another (latitude, longitude) point which is unknown during runtime. I got an API to calculate the distance in meters between their location and the given point. What I need eventually is to find the 5 closest agents to the given point.



What I did in order to achieve this is adding another class:



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);




And the usage:



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));



It works, but is there a more elegant way to do it? I'm not sure if adding another class might be a bit redundant since all it does is just adding a property for sorting, but adding the distance property to the Agent class, doesn't make so much sense.










share|improve this question











$endgroup$











  • $begingroup$
    @KonradRudolph what do you mean?
    $endgroup$
    – Yonatan Nir
    May 22 at 11:01










  • $begingroup$
    Never mind, my mistake.
    $endgroup$
    – Konrad Rudolph
    May 22 at 12:58













6












6








6





$begingroup$


I got a C# class:



public class Agent 

public string AgentId set; get;
public double Longitude set; get;
public double Latitude set; get;



There are several agents in several different locations. During the program's lifetime, they can receive a call to another (latitude, longitude) point which is unknown during runtime. I got an API to calculate the distance in meters between their location and the given point. What I need eventually is to find the 5 closest agents to the given point.



What I did in order to achieve this is adding another class:



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);




And the usage:



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));



It works, but is there a more elegant way to do it? I'm not sure if adding another class might be a bit redundant since all it does is just adding a property for sorting, but adding the distance property to the Agent class, doesn't make so much sense.










share|improve this question











$endgroup$




I got a C# class:



public class Agent 

public string AgentId set; get;
public double Longitude set; get;
public double Latitude set; get;



There are several agents in several different locations. During the program's lifetime, they can receive a call to another (latitude, longitude) point which is unknown during runtime. I got an API to calculate the distance in meters between their location and the given point. What I need eventually is to find the 5 closest agents to the given point.



What I did in order to achieve this is adding another class:



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);




And the usage:



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));



It works, but is there a more elegant way to do it? I'm not sure if adding another class might be a bit redundant since all it does is just adding a property for sorting, but adding the distance property to the Agent class, doesn't make so much sense.







c# sorting geospatial






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 22 at 6:13









200_success

133k20163433




133k20163433










asked May 21 at 17:08









Yonatan NirYonatan Nir

1384




1384











  • $begingroup$
    @KonradRudolph what do you mean?
    $endgroup$
    – Yonatan Nir
    May 22 at 11:01










  • $begingroup$
    Never mind, my mistake.
    $endgroup$
    – Konrad Rudolph
    May 22 at 12:58
















  • $begingroup$
    @KonradRudolph what do you mean?
    $endgroup$
    – Yonatan Nir
    May 22 at 11:01










  • $begingroup$
    Never mind, my mistake.
    $endgroup$
    – Konrad Rudolph
    May 22 at 12:58















$begingroup$
@KonradRudolph what do you mean?
$endgroup$
– Yonatan Nir
May 22 at 11:01




$begingroup$
@KonradRudolph what do you mean?
$endgroup$
– Yonatan Nir
May 22 at 11:01












$begingroup$
Never mind, my mistake.
$endgroup$
– Konrad Rudolph
May 22 at 12:58




$begingroup$
Never mind, my mistake.
$endgroup$
– Konrad Rudolph
May 22 at 12:58










3 Answers
3






active

oldest

votes


















7












$begingroup$


var agents = db.GetAgents();
if(agents == null || agents.Count == 0)

Console.WriteLine("No Results");




You should exit your application/code block when you detect an invalid state. Here even if "No Results" is printed, the app will still run into a NullReferenceException or IndexOutOfRangeException in the next few steps. Also, talking of IndexOutOfRangeException, there is no check against if there is at least 5 agents in your db.



Instead of creating an IComparable, you can just use linq to sort directly. But, since in your case, you also need to print out the distance (the value used for sorting), we will need to create an anonymous class to hold it:



var agents = db.GetAgents();
if(agents == null || agents.Count == 0)

Console.WriteLine("No Results");
return;


var nearestAgents = agents
.Select(x => new

x.AgentId,
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");



The .Take(5) ensures that only 5 agent will be printed out, or less.






share|improve this answer











$endgroup$








  • 1




    $begingroup$
    The anonymous class doesn't need the x.Latitude and x.Longitude fields.
    $endgroup$
    – 200_success
    May 22 at 6:16


















4












$begingroup$

I'd say you have the following options:




  1. Keep that new class if you think it's relevant in your business. Here, it should be important to know if the logic of calculate the distance should be on that web service (I assume that 'ws' variable means that) or within your model (i.e., the 'Agent' class).


  2. Use an anonymous class if you think you won't pass that info to another method.


  3. Use a dictionary if you think it's not relevant in your business but will pass that info to another method.

Personally, I'd go for the first one since it's the most natural to me. By the way, I'd use LINQ's OrderBy instead of implementing the IComparable interface; again, for expressiveness.






share|improve this answer









$endgroup$




















    0












    $begingroup$

    I think, I would hold on to your decorator pattern because it is more reusable than the selection with anonymous objects.



     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";




    You could extent Agent with some converter methods:



    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];




    where IDistanceProvider is



     public interface IDistanceProvider

    double GetDistance(Agent agent);




    In the concrete use case it ends up with code like this:



     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);



    which is easy to understand and maintain and you have a setup that can be used wherever needed.






    share|improve this answer











    $endgroup$













      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "196"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f220650%2ffinding-the-nearest-agents-to-a-customer-using-icomparable%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      7












      $begingroup$


      var agents = db.GetAgents();
      if(agents == null || agents.Count == 0)

      Console.WriteLine("No Results");




      You should exit your application/code block when you detect an invalid state. Here even if "No Results" is printed, the app will still run into a NullReferenceException or IndexOutOfRangeException in the next few steps. Also, talking of IndexOutOfRangeException, there is no check against if there is at least 5 agents in your db.



      Instead of creating an IComparable, you can just use linq to sort directly. But, since in your case, you also need to print out the distance (the value used for sorting), we will need to create an anonymous class to hold it:



      var agents = db.GetAgents();
      if(agents == null || agents.Count == 0)

      Console.WriteLine("No Results");
      return;


      var nearestAgents = agents
      .Select(x => new

      x.AgentId,
      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");



      The .Take(5) ensures that only 5 agent will be printed out, or less.






      share|improve this answer











      $endgroup$








      • 1




        $begingroup$
        The anonymous class doesn't need the x.Latitude and x.Longitude fields.
        $endgroup$
        – 200_success
        May 22 at 6:16















      7












      $begingroup$


      var agents = db.GetAgents();
      if(agents == null || agents.Count == 0)

      Console.WriteLine("No Results");




      You should exit your application/code block when you detect an invalid state. Here even if "No Results" is printed, the app will still run into a NullReferenceException or IndexOutOfRangeException in the next few steps. Also, talking of IndexOutOfRangeException, there is no check against if there is at least 5 agents in your db.



      Instead of creating an IComparable, you can just use linq to sort directly. But, since in your case, you also need to print out the distance (the value used for sorting), we will need to create an anonymous class to hold it:



      var agents = db.GetAgents();
      if(agents == null || agents.Count == 0)

      Console.WriteLine("No Results");
      return;


      var nearestAgents = agents
      .Select(x => new

      x.AgentId,
      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");



      The .Take(5) ensures that only 5 agent will be printed out, or less.






      share|improve this answer











      $endgroup$








      • 1




        $begingroup$
        The anonymous class doesn't need the x.Latitude and x.Longitude fields.
        $endgroup$
        – 200_success
        May 22 at 6:16













      7












      7








      7





      $begingroup$


      var agents = db.GetAgents();
      if(agents == null || agents.Count == 0)

      Console.WriteLine("No Results");




      You should exit your application/code block when you detect an invalid state. Here even if "No Results" is printed, the app will still run into a NullReferenceException or IndexOutOfRangeException in the next few steps. Also, talking of IndexOutOfRangeException, there is no check against if there is at least 5 agents in your db.



      Instead of creating an IComparable, you can just use linq to sort directly. But, since in your case, you also need to print out the distance (the value used for sorting), we will need to create an anonymous class to hold it:



      var agents = db.GetAgents();
      if(agents == null || agents.Count == 0)

      Console.WriteLine("No Results");
      return;


      var nearestAgents = agents
      .Select(x => new

      x.AgentId,
      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");



      The .Take(5) ensures that only 5 agent will be printed out, or less.






      share|improve this answer











      $endgroup$




      var agents = db.GetAgents();
      if(agents == null || agents.Count == 0)

      Console.WriteLine("No Results");




      You should exit your application/code block when you detect an invalid state. Here even if "No Results" is printed, the app will still run into a NullReferenceException or IndexOutOfRangeException in the next few steps. Also, talking of IndexOutOfRangeException, there is no check against if there is at least 5 agents in your db.



      Instead of creating an IComparable, you can just use linq to sort directly. But, since in your case, you also need to print out the distance (the value used for sorting), we will need to create an anonymous class to hold it:



      var agents = db.GetAgents();
      if(agents == null || agents.Count == 0)

      Console.WriteLine("No Results");
      return;


      var nearestAgents = agents
      .Select(x => new

      x.AgentId,
      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");



      The .Take(5) ensures that only 5 agent will be printed out, or less.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited May 22 at 16:47

























      answered May 21 at 18:09









      Xiaoy312Xiaoy312

      2,9961016




      2,9961016







      • 1




        $begingroup$
        The anonymous class doesn't need the x.Latitude and x.Longitude fields.
        $endgroup$
        – 200_success
        May 22 at 6:16












      • 1




        $begingroup$
        The anonymous class doesn't need the x.Latitude and x.Longitude fields.
        $endgroup$
        – 200_success
        May 22 at 6:16







      1




      1




      $begingroup$
      The anonymous class doesn't need the x.Latitude and x.Longitude fields.
      $endgroup$
      – 200_success
      May 22 at 6:16




      $begingroup$
      The anonymous class doesn't need the x.Latitude and x.Longitude fields.
      $endgroup$
      – 200_success
      May 22 at 6:16













      4












      $begingroup$

      I'd say you have the following options:




      1. Keep that new class if you think it's relevant in your business. Here, it should be important to know if the logic of calculate the distance should be on that web service (I assume that 'ws' variable means that) or within your model (i.e., the 'Agent' class).


      2. Use an anonymous class if you think you won't pass that info to another method.


      3. Use a dictionary if you think it's not relevant in your business but will pass that info to another method.

      Personally, I'd go for the first one since it's the most natural to me. By the way, I'd use LINQ's OrderBy instead of implementing the IComparable interface; again, for expressiveness.






      share|improve this answer









      $endgroup$

















        4












        $begingroup$

        I'd say you have the following options:




        1. Keep that new class if you think it's relevant in your business. Here, it should be important to know if the logic of calculate the distance should be on that web service (I assume that 'ws' variable means that) or within your model (i.e., the 'Agent' class).


        2. Use an anonymous class if you think you won't pass that info to another method.


        3. Use a dictionary if you think it's not relevant in your business but will pass that info to another method.

        Personally, I'd go for the first one since it's the most natural to me. By the way, I'd use LINQ's OrderBy instead of implementing the IComparable interface; again, for expressiveness.






        share|improve this answer









        $endgroup$















          4












          4








          4





          $begingroup$

          I'd say you have the following options:




          1. Keep that new class if you think it's relevant in your business. Here, it should be important to know if the logic of calculate the distance should be on that web service (I assume that 'ws' variable means that) or within your model (i.e., the 'Agent' class).


          2. Use an anonymous class if you think you won't pass that info to another method.


          3. Use a dictionary if you think it's not relevant in your business but will pass that info to another method.

          Personally, I'd go for the first one since it's the most natural to me. By the way, I'd use LINQ's OrderBy instead of implementing the IComparable interface; again, for expressiveness.






          share|improve this answer









          $endgroup$



          I'd say you have the following options:




          1. Keep that new class if you think it's relevant in your business. Here, it should be important to know if the logic of calculate the distance should be on that web service (I assume that 'ws' variable means that) or within your model (i.e., the 'Agent' class).


          2. Use an anonymous class if you think you won't pass that info to another method.


          3. Use a dictionary if you think it's not relevant in your business but will pass that info to another method.

          Personally, I'd go for the first one since it's the most natural to me. By the way, I'd use LINQ's OrderBy instead of implementing the IComparable interface; again, for expressiveness.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 21 at 18:08









          A Bravo DevA Bravo Dev

          639211




          639211





















              0












              $begingroup$

              I think, I would hold on to your decorator pattern because it is more reusable than the selection with anonymous objects.



               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";




              You could extent Agent with some converter methods:



              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];




              where IDistanceProvider is



               public interface IDistanceProvider

              double GetDistance(Agent agent);




              In the concrete use case it ends up with code like this:



               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);



              which is easy to understand and maintain and you have a setup that can be used wherever needed.






              share|improve this answer











              $endgroup$

















                0












                $begingroup$

                I think, I would hold on to your decorator pattern because it is more reusable than the selection with anonymous objects.



                 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";




                You could extent Agent with some converter methods:



                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];




                where IDistanceProvider is



                 public interface IDistanceProvider

                double GetDistance(Agent agent);




                In the concrete use case it ends up with code like this:



                 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);



                which is easy to understand and maintain and you have a setup that can be used wherever needed.






                share|improve this answer











                $endgroup$















                  0












                  0








                  0





                  $begingroup$

                  I think, I would hold on to your decorator pattern because it is more reusable than the selection with anonymous objects.



                   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";




                  You could extent Agent with some converter methods:



                  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];




                  where IDistanceProvider is



                   public interface IDistanceProvider

                  double GetDistance(Agent agent);




                  In the concrete use case it ends up with code like this:



                   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);



                  which is easy to understand and maintain and you have a setup that can be used wherever needed.






                  share|improve this answer











                  $endgroup$



                  I think, I would hold on to your decorator pattern because it is more reusable than the selection with anonymous objects.



                   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";




                  You could extent Agent with some converter methods:



                  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];




                  where IDistanceProvider is



                   public interface IDistanceProvider

                  double GetDistance(Agent agent);




                  In the concrete use case it ends up with code like this:



                   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);



                  which is easy to understand and maintain and you have a setup that can be used wherever needed.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 22 at 7:33

























                  answered May 22 at 5:58









                  Henrik HansenHenrik Hansen

                  9,04211332




                  9,04211332



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Code Review Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      Use MathJax to format equations. MathJax reference.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f220650%2ffinding-the-nearest-agents-to-a-customer-using-icomparable%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Club Baloncesto Breogán Índice Historia | Pavillón | Nome | O Breogán na cultura popular | Xogadores | Adestradores | Presidentes | Palmarés | Historial | Líderes | Notas | Véxase tamén | Menú de navegacióncbbreogan.galCadroGuía oficial da ACB 2009-10, páxina 201Guía oficial ACB 1992, páxina 183. Editorial DB.É de 6.500 espectadores sentados axeitándose á última normativa"Estudiantes Junior, entre as mellores canteiras"o orixinalHemeroteca El Mundo Deportivo, 16 setembro de 1970, páxina 12Historia do BreogánAlfredo Pérez, o último canoneiroHistoria C.B. BreogánHemeroteca de El Mundo DeportivoJimmy Wright, norteamericano do Breogán deixará Lugo por ameazas de morteResultados de Breogán en 1986-87Resultados de Breogán en 1990-91Ficha de Velimir Perasović en acb.comResultados de Breogán en 1994-95Breogán arrasa al Barça. "El Mundo Deportivo", 27 de setembro de 1999, páxina 58CB Breogán - FC BarcelonaA FEB invita a participar nunha nova Liga EuropeaCharlie Bell na prensa estatalMáximos anotadores 2005Tempada 2005-06 : Tódolos Xogadores da Xornada""Non quero pensar nunha man negra, mais pregúntome que está a pasar""o orixinalRaúl López, orgulloso dos xogadores, presume da boa saúde económica do BreogánJulio González confirma que cesa como presidente del BreogánHomenaxe a Lisardo GómezA tempada do rexurdimento celesteEntrevista a Lisardo GómezEl COB dinamita el Pazo para forzar el quinto (69-73)Cafés Candelas, patrocinador del CB Breogán"Suso Lázare, novo presidente do Breogán"o orixinalCafés Candelas Breogán firma el mayor triunfo de la historiaEl Breogán realizará 17 homenajes por su cincuenta aniversario"O Breogán honra ao seu fundador e primeiro presidente"o orixinalMiguel Giao recibiu a homenaxe do PazoHomenaxe aos primeiros gladiadores celestesO home que nos amosa como ver o Breo co corazónTita Franco será homenaxeada polos #50anosdeBreoJulio Vila recibirá unha homenaxe in memoriam polos #50anosdeBreo"O Breogán homenaxeará aos seus aboados máis veteráns"Pechada ovación a «Capi» Sanmartín e Ricardo «Corazón de González»Homenaxe por décadas de informaciónPaco García volve ao Pazo con motivo do 50 aniversario"Resultados y clasificaciones""O Cafés Candelas Breogán, campión da Copa Princesa""O Cafés Candelas Breogán, equipo ACB"C.B. Breogán"Proxecto social"o orixinal"Centros asociados"o orixinalFicha en imdb.comMario Camus trata la recuperación del amor en 'La vieja música', su última película"Páxina web oficial""Club Baloncesto Breogán""C. B. Breogán S.A.D."eehttp://www.fegaba.com

                      Vilaño, A Laracha Índice Patrimonio | Lugares e parroquias | Véxase tamén | Menú de navegación43°14′52″N 8°36′03″O / 43.24775, -8.60070

                      Cegueira Índice Epidemioloxía | Deficiencia visual | Tipos de cegueira | Principais causas de cegueira | Tratamento | Técnicas de adaptación e axudas | Vida dos cegos | Primeiros auxilios | Crenzas respecto das persoas cegas | Crenzas das persoas cegas | O neno deficiente visual | Aspectos psicolóxicos da cegueira | Notas | Véxase tamén | Menú de navegación54.054.154.436928256blindnessDicionario da Real Academia GalegaPortal das Palabras"International Standards: Visual Standards — Aspects and Ranges of Vision Loss with Emphasis on Population Surveys.""Visual impairment and blindness""Presentan un plan para previr a cegueira"o orixinalACCDV Associació Catalana de Cecs i Disminuïts Visuals - PMFTrachoma"Effect of gene therapy on visual function in Leber's congenital amaurosis"1844137110.1056/NEJMoa0802268Cans guía - os mellores amigos dos cegosArquivadoEscola de cans guía para cegos en Mortágua, PortugalArquivado"Tecnología para ciegos y deficientes visuales. Recopilación de recursos gratuitos en la Red""Colorino""‘COL.diesis’, escuchar los sonidos del color""COL.diesis: Transforming Colour into Melody and Implementing the Result in a Colour Sensor Device"o orixinal"Sistema de desarrollo de sinestesia color-sonido para invidentes utilizando un protocolo de audio""Enseñanza táctil - geometría y color. Juegos didácticos para niños ciegos y videntes""Sistema Constanz"L'ocupació laboral dels cecs a l'Estat espanyol està pràcticament equiparada a la de les persones amb visió, entrevista amb Pedro ZuritaONCE (Organización Nacional de Cegos de España)Prevención da cegueiraDescrición de deficiencias visuais (Disc@pnet)Braillín, un boneco atractivo para calquera neno, con ou sen discapacidade, que permite familiarizarse co sistema de escritura e lectura brailleAxudas Técnicas36838ID00897494007150-90057129528256DOID:1432HP:0000618D001766C10.597.751.941.162C97109C0155020