0

我在使用 LINQ to 已解决的实体时遇到问题,但想确保我以正确的方式解决了问题。

我有 2 节课:

namespace ShopTest.Models
{

public class Shop
{
    public int ShopID { get; set; }
    public string Name { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Postcode { get; set; }
    public string Country { get; set; }
    public decimal Latitude { get; set; }
    public decimal Longitude{ get; set; }
}

[NotMapped]
public class ShopLocation : Shop
{
    public decimal AddressLatitude { get; set; }
    public decimal AddressLongitude { get; set; }
    public decimal DistanceFromAddress
    {
        get
        {
            return Convert.ToDecimal(
                        Math.Sqrt(
                                Math.Pow(Convert.ToDouble(this.Latitude - this.AddressLatitude), 2.0)
                                    +
                                Math.Pow(Convert.ToDouble(this.Longitude- this.AddressLongitude), 2.0)
                        )
                        * 62.1371192
                    );
        }
    }
}

}

在 LINQ 中查询时,我最初尝试过:

decimal lat = Convert.ToDecimal(-33.8736510, NumberFormatInfo.InvariantInfo);
decimal lng = Convert.ToDecimal(151.2068896, NumberFormatInfo.InvariantInfo);

var nearbyShops = from c in db.Shops
                   where Math.Abs(c.lat - lat) < 0.25M &&
                         Math.Abs(c.lng - lng) < 0.25M
                   select new NearbyShopLocation()
                   {
                       StoreNumber = store.StoreNumber,
                       Address = store.Address,
                       City = store.City,
                       Region = store.Region,
                       CountryCode = store.CountryCode,
                       PostalCode = store.PostalCode,
                       Latitude = store.Latitude,
                       Longitude = store.Longitude,
                       AddressLatitude = lat,
                       AddressLongitude = lng
                   };

var nearbySortedShops = nearbyShops.ToList().OrderBy(s => s.DistanceFromAddress).ToList();

但是我不断收到错误“无法在 LINQ to Entities 查询中构造实体或复杂类型'ShopTest.Controllers.Shops'”

我已经用下面的代码解决了这个问题,但它为什么会起作用是没有意义的——作为 MVC 的新手,我希望有人能解释一下。:-)

var nearbyShops = (from c in db.Shops
                  where Math.Abs(c.lat - lat) < 0.25M &&
                        Math.Abs(c.lng - lng) < 0.25M
                  select new 
                  {
                      StoreNumber = c.StoreNumber,
                      Address = c.Address,
                      City = c.City,
                      Country = c.Country,
                      PostalCode = c.PostalCode,
                      Latitude = c.Latitude,
                      Longitude = c.Longitude,
                  }).ToList().Select(l => new ShopLocation
                  {
                       Name = l.Name,
                       City = l.City,
                       State = l.State,
                       Country = l.Country,
                       Lat = l.Lat,
                       Lng = l.Lng,
                       AddressLatitude = lat,
                       AddressLongitude = lng
                  }).ToList().OrderBy(s => s.DistanceFromAddress).ToList();

我做对了吗?有没有更好的办法?

4

1 回答 1

1

EF 具有不能在查询中手动构建映射实体的限制。这意味着你不能这样做:

 var shops = from s in db.Shops where ... select new Shop { ... };

这也涉及派生实体。因此,您首先必须调用 ToList 才能切换到 Linq-to-Objects:

 var shopse = db.Shops.Where(...).ToList().Select(s => new Shop { ... });

通常,您应该可以:

var nearbyShops = 
             (from c in db.Shops
              where Math.Abs(c.lat - lat) < 0.25M &&
                    Math.Abs(c.lng - lng) < 0.25M
              select c).ToList()
             .Select(l => new ShopLocation
              {
                   Name = l.Name,
                   City = l.City,
                   State = l.State,
                   Country = l.Country,
                   Lat = l.Lat,
                   Lng = l.Lng,
                   AddressLatitude = lat,
                   AddressLongitude = lng
              }).OrderBy(s => s.DistanceFromAddress).ToList();
于 2012-04-25T07:35:24.130 回答