2012-04-25 13 views
0

私は解決したエンティティに対してLINQに問題がありますが、正しい方法で問題を解決したいと考えています。 LINQでクエリを実行すると、私はもともと試みたASP.NET MVCクラスの継承とLINQ

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

} 

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(); 

は、しかし、私はエラー「エンティティまたは複合型「ShopTest.Controllers.Shopsを取得保管

は、私は2つのクラスを持っています'LINQ to Entitiesクエリで構築できません "

私は以下のコードでこの問題を修正しましたが、これがうまくいかない理由がわかりません - 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(); 

これを正しく実行しましたか?より良い方法がありますか?

答えて

1

EFには、クエリで手動でマッピングされたエンティティを作成できないという制限があります。つまり、これを行うことはできません。

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

これには派生したエンティティも含まれます。そのため、あなたは最初のLINQツーオブジェクトに切り替えることToListメソッドを呼び出す必要があります:

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

は、一般的にあなたがOKでなければなりません:非常に参考と教育

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(); 
+0

グレート応答、および完全に働きました。ありがとうございました! –