Entity framework ASP.NET MVC类继承和LINQ

Entity framework ASP.NET MVC类继承和LINQ,entity-framework,linq-to-entities,Entity Framework,Linq To Entities,我已经解决了LINQ to实体的一个问题,但是我想确保我以正确的方式解决了这个问题 我有两门课: 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;

我已经解决了LINQ to实体的一个问题,但是我想确保我以正确的方式解决了这个问题

我有两门课:

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();
decimal lat=Convert.ToDecimal(-33.8736510,NumberFormatInfo.InvariantInfo);
十进制lng=转换为十进制(151.2068896,NumberFormatInfo.InvariantInfo);
var nearbyShops=以分贝为单位的c
式中,Math.Abs(c.lat-lat)<0.25M&&
数学绝对值(c.lng-lng)<0.25M
选择新的NearbyShopLocation()
{
StoreNumber=store.StoreNumber,
地址=商店地址,
城市=商店。城市,
Region=store.Region,
CountryCode=store.CountryCode,
PostalCode=store.PostalCode,
纬度=商店。纬度,
经度=存储。经度,
地址纬度=纬度,
地址经度=液化天然气
};
var nearbySortedShops=nearbyShops.ToList().OrderBy(s=>s.DistanceFromAddress.ToList();
但是,我不断收到错误“无法在LINQ to Entities查询中构造实体或复杂类型'ShopTest.Controllers.Shops'

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

var nearbyShops=(来自db.Shops中的c)
式中,Math.Abs(c.lat-lat)<0.25M&&
数学绝对值(c.lng-lng)<0.25M
选择新的
{
StoreNumber=c.StoreNumber,
地址,地址,
城市,
国家,国家,
PostalCode=c.PostalCode,
纬度=c.纬度,
经度,经度,
}).ToList().Select(l=>newshopLocation
{
名称=l.名称,
城市=l.城市,
状态=l.状态,
国家=l.国家,
Lat=l.Lat,
液化天然气=l.液化天然气,
地址纬度=纬度,
地址经度=液化天然气
}).ToList().OrderBy(s=>s.DistanceFromAddress).ToList();

我做得对吗?有更好的方法吗?

EF有一个限制,即您不能在查询中手动构建映射实体。这意味着您不能这样做:

 var shops = from s in db.Shops where ... select new Shop { ... };
这也涉及衍生实体。因此,首先必须调用ToList以切换到Linq to对象:

 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();
var nearbyShops=
(来自数据库商店中的c)
式中,Math.Abs(c.lat-lat)<0.25M&&
数学绝对值(c.lng-lng)<0.25M
选择c.ToList()
.选择(l=>new ShopLocation
{
名称=l.名称,
城市=l.城市,
状态=l.状态,
国家=l.国家,
Lat=l.Lat,
液化天然气=l.液化天然气,
地址纬度=纬度,
地址经度=液化天然气
}).OrderBy(s=>s.DistanceFromAddress).ToList();

反响很好,很有帮助,也很有教育意义,效果很好。非常感谢。
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();