简体   繁体   中英

How do I capture a Linq (Method Syntax) value that's created in the where clause?

I'm sure this has been answered, I just can't find it. Anyhow, I have a Linq statement where I compare a GeoCoorinate with another GeoCoordinate base on user input:

var agents = db.AllAgentLocations()
                    .AsEnumerable()
                    .Where(al => al.PrimaryOffice)
                    .Where(al => (25 >= (searchCoords.GetDistanceTo(
                                new GeoCoordinate
                                {
                                    Latitude = double.Parse(al.Location.Latitude),
                                    Longitude = double.Parse(al.Location.Longitude)
                                }
                        ) / 1609.34)))
                        .Select(a => a.Agent);

The query works just fine and I get the expected output of an IEnumerable. I am trying to figure out a way to output the distance generated by

(25 >= (searchCoords.GetDistanceTo(
      new GeoCoordinate
      {
          Latitude = double.Parse(al.Location.Latitude),
          Longitude = double.Parse(al.Location.Longitude)
      }
      ) / 1609.34)

I am looking to put both the agent(s) and distance(s) into an IEnumerable Which looks like so:

public class AgentDistanceViewModel
{
    public Agent Agent { get; set; }
    public double Distance { get; set; }
}

Just add a projection (Select) before your Where clause to transform the input objects into a new object, then you can apply the Where clause to the new object instead:

var agents = db.AllAgentLocations()
    .AsEnumerable()
    .Where(al => al.PrimaryOffice)
    .Select(al => new AgentDistanceViewModel {
        Agent = al.Agent,
        Distance = searchCoords.GetDistanceTo(
            new GeoCoordinate {
                Latitude = double.Parse(al.Location.Latitude),
                Longitude = double.Parse(al.Location.Longitude)
            }
        ) / 1609.34
    })
    .Where(dvm => 25 >= dvm.Distance)
    // ...

It's more helpful to think about Linq queries not as "SQL for C#" but as a framework for applying transforms to streams of objects. You can apply projection, filtering, ordering, etc. at any point, and in any order. If the result at the end is missing some information, then just add a transform somewhere to add it!

You could project the values to the target type before filtering:

var agents = db.AllAgentLocations().AsEnumerable()
                .Where(al => al.PrimaryOffice)
                .Select(al => new AgentDistanceViewModel
                          {
                               Agent = al.Agent,
                               Distance = searchCoords.GetDistanceTo(
                                      new GeoCoordinate
                                      {
                                         Latitude = double.Parse(al.Location.Latitude),
                                         Longitude = double.Parse(al.Location.Longitude)
                            }) / 1609.34
                          })
                    .Where(a => a.Distance < 25);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM