2016-10-22 76 views
0

我想用linq獲取數據列表,但我得到這個錯誤。無法創建類型爲「Domain.DataModel.Hospital」的常量值。在這種情況下只支持基本類型或枚舉類型

不久,我的問題是:

我有一個Hospital模型。該模型由城市code.All城市代碼爲十進制類型。(1,2,3,4,...等)

另外我有一個PersonPersonStatu models.Person和PersonStatu與TcKimlik物業相關。和Hospital模型PersonStatuHospitalCode財產。

所以,當我試圖去通過城市代碼的醫院,如果城市代碼大於10時,小於10就不可能返回任何data.And它出現此錯誤是works.But:

Unable to create a constant value of type 'Domain.DataModel.Hospital'. Only primitive types or enumeration types are supported in this context. 

這裏是我的Hospital型號:

[Table("SPTS.HOSPITALS")] 
public partial class Hospital 
{ 
     .... 

    public decimal? IL_KODU { get; set; } 

     .... 

} 

這裏是我的Querable LINQ查詢:

var hospitals = context.Hospitals.Where(p=>p.IL_KODU==1); 


    var persons = (from c in context.Persons 
        join x in context.PersonStatus on c.TcKimlik equals x.Tckn 
        join h in hospitals on x.HospitalCode equals h.KURUM_KODU 
        where x.Statu == true 
        select new DataViewModel 
        { 
         Id = c.Id, 
         TcKimlik = c.TcKimlik, 
         Uyruk = c.Uyruk, 
         Ad = c.Ad, 
         Soyad = c.Soyad, 
         Cinsiyet = c.Cinsiyet, 
         DogumTarihi = c.DogumTarihi, 
         KurumStatu = h.PAYDAS, 
         KurumKodu = h.KURUM_KODU, 
         KurumAdi = h.KURUM_ADI, 
         BranchName = c.Brans.BranchName, 
         AcademicTitleName = c.AkademikUnvan.Title, 
         ManagerialTitleName = c.IdariUnvan.Title, 
         StaffStatuName = c.Durum.Statu, 
         BranchTypeName = c.Unvan.Type, 
         ServiceClassName = c.Unvan.ServiceClass.Name, 
         City = h.KURUM_ILI, 
         CityCode = h.IL_KODU, 
         CityTownName = h.KURUM_ILCESI 
        }).AsQueryable(); 

那麼我能做些什麼來解決這個問題?

任何幫助,將不勝感激。

感謝。

回答

0

您的問題,從這個行開始:

var hospitals = context.Hospitals.Where(p=>p.IL_KODU==1); 

該行創建IQueryable收集從IL_KODU比賽1,因此你不能簡單地執行與另一組對數據庫上下文連接數據集Hospitals數據上下文數據保存在內存中。

要使用多個連接,從「Showing Unable to create a constant value of type ''. Only primitive types ('such as Int32, String, and Guid') are supported in this context」基於處理Hospitals收集,把Where方法直接在第二個連接,而不是:

var persons = (from c in context.Persons 
       join x in context.PersonStatus 
       on c.TcKimlik equals x.Tckn 
       join h in context.Hospitals.Where(p => p.IL_KODU == 1) // where clause added here using database context 
       on x.HospitalCode equals h.KURUM_KODU 
       where x.Statu == true 
       select new DataViewModel 
       { 
         Id = c.Id, 
         TcKimlik = c.TcKimlik, 
         Uyruk = c.Uyruk, 
         Ad = c.Ad, 
         Soyad = c.Soyad, 
         Cinsiyet = c.Cinsiyet, 
         DogumTarihi = c.DogumTarihi, 
         KurumStatu = h.PAYDAS, 
         KurumKodu = h.KURUM_KODU, 
         KurumAdi = h.KURUM_ADI, 
         BranchName = c.Brans.BranchName, 
         AcademicTitleName = c.AkademikUnvan.Title, 
         ManagerialTitleName = c.IdariUnvan.Title, 
         StaffStatuName = c.Durum.Statu, 
         BranchTypeName = c.Unvan.Type, 
         ServiceClassName = c.Unvan.ServiceClass.Name, 
         City = h.KURUM_ILI, 
         CityCode = h.IL_KODU, 
         CityTownName = h.KURUM_ILCESI 
       }).AsQueryable(); 

注意:如果修改後的查詢仍然無法工作,請嘗試換context.Personscontext.PersonStatus互相排列,包括它們之間的on子句關係。

相關問題