Question

Alright folks, I am a newcomer to Data Services and LINQ in desperate need of some guidance. In just a few days I have encountered numerous unexpected obstacles and I'm stuck on one now. I'm hoping these are just typical frustrations of learning a new tool.

I have a WCF Data Service for serving up data from a Sql Server database table of Gps coordinates. In particular I have a service operation method that allows you to specify a decimal precision and latitude/longitude range to produce a more general representation of the data.

In a web browser it appears to work just as expected. However when I try to call the operation from my client application, the list returned to the client differs from the list generated by the service.

I will use pieces of my code to explain the details:

Data Service Operation:

    // This is my service operation that I need to call from my client app (see below). 
    // It should return an IEnumerable<Gps> (Gps is one of my Entity Model 
    // types) list of distinct GPS rounded to the number of decimal positions 
    // specified and within the range specified.
    [WebGet]
    public IEnumerable<Gps> GetGpsView(int decimalPlaces, decimal minLatitude, decimal minLongitude, decimal maxLatitude, decimal maxLongitude)
    {
        // I must first return a list of anonymous-type objects
        // because LINQ does not seem to allow me to construct my
        // Gps object within the query (one of those other issues
        // I had to tip-toe around).
        var list = (from g in this.CurrentDataSource.Gps
                    where g.Latitude >= minLatitude &&
                             g.Latitude <= maxLatitude &&
                             g.Longitude >= minLongitude &&
                             g.Longitude <= maxLongitude
                    select new
                    {
                        Id = 0,
                        Latitude = Math.Round(g.Latitude, decimalPlaces),
                        Longitude = Math.Round(g.Longitude, decimalPlaces)
                    }).Distinct().ToList();

        // Now that I have my results, I need to convert the items in the
        // list to my Gps entity object.
        IEnumerable<Gps> gpsList = list.ConvertAll<Gps>(item => new Gps
                            {
                                Id = item.Id,
                                Latitude = item.Latitude,
                                Longitude = item.Longitude
                            });

        return gpsList;
    }

If I debug the above method (running it on Visual Studio's virtual server) when called from my client app, gpsList appears to contain the proper data just before returning to the client. Using my test parameters, I get a list of 200 distinct Gps objects whose values are rounded to the decimal places I specify.

However, once the results are returned to the calling method in my client app, I have a list of 200 Gps objects, but they are all the SAME value. To be specific, the duplicated value is the LAST value in my expected result set. I confirmed this by calling this operation in a web browser and viewing the results.

Client Method:

// Partial class extension of code auto-generated by service reference.
public partial class HsiSideBySideEntities
{
    public List<Gps> GetGpsView(int decimalPlaces, decimal minLatitude, decimal minLongitude, decimal maxLatitude, decimal maxLongitude)
    {
        this.IgnoreMissingProperties = true;

        // Format my relative URI string.
        string uri = string.Format("/GetGpsView?decimalPlaces={0}&minLatitude={1}M&minLongitude={2}M&maxLatitude={3}M&maxLongitude={4}M", decimalPlaces, minLatitude, minLongitude, maxLatitude, maxLongitude);

        // If I debug both client and service at the same time, when I step over this
        // line, it does reach my data service - and as I mentioned above, on the
        // service end it appears to generate the correct results.
        List<Gps> gpsList = this.Execute<Gps>(new Uri(uri, UriKind.Relative)).ToList();

        // However, the results are returned to the client code, my list contains
        // duplicates of the last expected record.
        return gpsList;
    }
}

I tried removing the "ToList()" portion of the "Execute()" line, but when I try to view the result set in the debugger, it shows an exception that reads, "Only a single enumeration is supported by this IEnumerable."

As far as I can tell, my client code is first suspect. After all, every other test shows that my data service operation is producing the desired results.

Do I need to do something different to obtain the IEnumerable list of objects from the data service?

I understand there is a CreateQuery() option, but I've read that Execute() is the more appropriate route for this scenario.

Was it helpful?

Solution

It's probably because of this:

select new
{
    Id = 0,
    Latitude = Math.Round(g.Latitude, decimalPlaces),
    Longitude = Math.Round(g.Longitude, decimalPlaces)
}

I assume that the Id property of the Gps entity is your primary key. In your example you are setting the Id of each returned Gps to zero. In the WCF Data Services client library, entities with the same primary key are treated as the same instance both for change tracking reasons and so that object graphs behave as you would expect in an object-oriented, reference-tracked environment such as .NET.

If for some reason you can't give the Gps entities a unique Id, consider using a Guid for the primary key.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top