无法创建类型为'domain . datmodel . hospital '的常量值.在此上下文中只支持

本文关键字:支持 上下文 常量 datmodel 类型 创建 domain hospital | 更新日期: 2023-09-27 18:14:29

我正在尝试使用linq获取数据列表,但我得到此错误。

我的问题是:

我有一个Hospital模型。这个模型由城市代码组成。所有的城市代码都是十进制的(1,2,3,4,…)等)

我也有一个PersonPersonStatu模型。与TcKimlik财产相关的人与人状态。与PersonStatuHospitalCode属性相关的Hospital模型

所以当我试着通过城市代码去医院时,如果城市代码大于10,它就能工作。但是当它小于10时,它不能返回任何数据。出现如下错误:

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

那么我该怎么解决这个问题呢?

如有任何帮助,不胜感激。

谢谢。

无法创建类型为'domain . datmodel . hospital '的常量值.在此上下文中只支持

你的问题从这行开始:

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

这一行从Hospitals数据上下文创建IQueryable集合,IL_KODU匹配1,因此您不能简单地将数据库上下文中的数据集与存储在内存中的另一组数据集连接起来。

处理Hospitals集合使用多个连接,基于"显示无法创建类型为''的常量值"。在此上下文中只支持基本类型('如Int32, String和guide '),将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子句关系。