好吧,我是数据服务和LINQ的新手,迫切需要一些指导。在短短几天内,我遇到了许多意外的障碍,现在我陷入了困境。我希望这些只是学习新工具的典型挫败感。

我有一个WCF数据服务,用于从SQL Server数据库表格GPS坐标表提供数据服务。特别是我有一种服务操作方法,它允许您指定十进制精度和纬度/经度范围,以产生数据的更一般表示。

在网络浏览器中,它似乎正如预期的那样工作。但是,当我尝试从客户端应用程序调用操作时,列表返回给客户端的列表与服务生成的列表不同。

我将使用我的代码部分来解释细节:

数据服务操作:

    // 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;
    }

如果我从我的客户端应用程序调用上述方法(在Visual Studio的虚拟服务器上运行它),则GPSLIST似乎在返回客户端之前似乎包含了适当的数据。使用我的测试参数,我获得了200个不同的GPS对象的列表,其值将其舍入到我指定的十进制位置。

但是,一旦结果将结果返回到我的客户端应用程序中的调用方法,我就有200个GPS对象的列表,但是它们都是相同的值。具体而言,重复的值是我预期结果集中的最后值。我通过在Web浏览器中调用此操作并查看结果来确认这一点。

客户端方法:

// 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;
    }
}

我尝试删除“ execute()”行的“ tolist()”部分,但是当我尝试查看调试器中的结果设置时,它显示了一个例外,读取了“只有单个枚举。 “

据我所知,我的客户代码是首先嫌疑。毕竟,所有其他测试都表明我的数据服务操作正在产生所需的结果。

我需要做一些不同的事情以获取数据服务中的对象列表吗?

我知道有一个createquery()选项,但是我已经读到execute()是适合这种情况的途径。

有帮助吗?

解决方案

可能是因为这样:

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

我认为GPS实体的ID属性是您的主要密钥。在您的示例中,您将每个返回的GPS的ID设置为零。在WCF数据服务客户库中,具有相同主键的实体被视为同一实例,既出于变化跟踪原因,又将对象图形表现出您在面向对象的,以对象的参考环境(例如.NET)中所期望的。

如果由于某种原因无法给GPS实体一个唯一的ID,请考虑使用主键的GUID。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top