views:

365

answers:

2

Hi,

Bumped into a strange situation here. Trying to extract cars from a sql database within a certain range of a position (lat/long) and return an IQueryable to do additional logic on the resultset afterwards.

public IQueryable<Car> QueryAllCarsByDistance(float latitude, float longitude, int distance)
{
    var cars = from car in QueryAllCars()
               join i in sqlContext.QueryContactsByDistance(latitude, longitude, distance)
               on car.ContactId equals i.Id
               orderby i.Distance
               select car;

    return cars;
}

QueryContactByDistance beeing a function in the SQL database and QueryAllCars just returns all cars from database.

My question is now: How do I append the Distance (i.Distance) to car? I don't want to do a select new Car { Distance = i.Distance, Id = car.Id, ... }. But how to just append this additional value to the car object?

+1  A: 

Unfortunately there is no way to do this. For most normal coding purposes, the type definitions emitted by C# and VB.Net are immutable. Fields and Properties cannot be added or removed from them at runtime.

All you can do is create a new type which wraps the Car type and provides the additional property. Or create an AnonymousType which has all of the properties needed.

JaredPar
This just made me think of an idea, anonymous derived classes, could be interesting :)
leppie
@leppie yes but that would make the inability to name types even worse :).
JaredPar
Exactly - those elements latitude, longitude and distance really don't belong to the car object per se - I'd create a wrapper that contains both the car plus these additional properties.
marc_s
A: 

One way around this is that you could setup a partial class for this (if you havn't already) add a new property to it called DistanceToReferencePoint, then in query set that property to distance, it then would not be saved in the database but would give you access to it.

Solmead
Allmost what I'm looking for. How would the select statement look like?
Tim