无法将类型“ System.Nullable”转换为类型“ System.Object”。 LINQ to Entities仅支持强制转换EDM基本类型或枚举类型

时间:2018-12-07 07:08:32

标签: model-view-controller

我收到的错误是:-

Unable to cast the type `'System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'` to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types.'

代码如下:-

List < SeoTreatmentTableItemModel> retlist = new List < SeoTreatmentTableItemModel>();


var result1 = from s in entities.Seotreatment

             join sc in entities.Seochannellink.Where(sc => sc.Publishdate >= 
             startdate && sc.Publishdate < enddate) on s.Seotreatmentid equals 
             sc.Seotreatmentid into GSeoChannelLink

             join ph in entities.PackageHeader on s.Seotreatmentid equals 
             ph.packageheaderId
             join c in entities.IndhealClickAnalytics.Where(c => c.Createddate 
             >= startdate && c.Createddate < enddate) on ph.Name equals 
             c.Reference into GIndhealClickAnalytics

             join l in entities.Leads.Where(l => l.Requestdate >= startdate && 
             l.Requestdate < enddate)
             on s.Seotreatmentid equals l.FK_packageheaderid into GLeads

            join e in entities.Enquiry.Where(e => e.Requestdate >= startdate && 
            e.Requestdate < enddate) on s.Seotreatmentid equals 
            e.FK_packageheaderid into GEnquiry

            join t in entities.Enquiry.Where(t => t.Requestdate >= startdate && 
            t.Requestdate < enddate && t.FK_statusid == 16)
            on s.Seotreatmentid equals t.FK_packageheaderid into GTreatments

            join p in entities.Package.Where(p=> p.Valid == 1) on 
            s.Seotreatmentid equals p.FKPackageHeader
            join h in entities.Hospital on p.FKHospital equals h.IDHospital into 
            GHospitals

            where s.Valid == true
            select new SeoTreatmentTableItemModel
            {
                Seotreatmentid = s.Seotreatmentid,
                Name = s.Name,
                Departmentid = s.Departmentid,
                Isayurveda = s.Isayurveda == true ? true : false,
                Articlepublished = s.Articlepublished == true ? true : false,
                Priority = s.Priority,
                Articlepublishdate = s.Articlepublishdate,
                Googlerank = s.Googlerank,
                Qrsummary = s.Qrsummary == true ? true : false,
                Istreatment = s.Istreatment == true ? true : false,
                Articles = GSeoChannelLink.Count(),
                Hits = GIndhealClickAnalytics.Count(),
                Leads = GLeads.Count(),
                Enquiries = GEnquiry.Count(),
                Treatments = GTreatments.Count(),
                Hospitals = GHospitals.Count(),
                Cost = ph.Costusdmin + "-" + ph.Costusdmax

              };

                if (result1 == null) return null;

                retlist = result1.ToList();

我正在最后一行。此处-retlist = result1.ToList();

1 个答案:

答案 0 :(得分:0)

在声明中

Seotreatmentid = s.Seotreatmentid,
                Name = s.Name,
                Departmentid = s.Departmentid,
                Isayurveda = s.Isayurveda == true ? true : false,
                Articlepublished = s.Articlepublished == true ? true : false,
                Priority = s.Priority,
                Articlepublishdate = s.Articlepublishdate,
                Googlerank = s.Googlerank,
                Qrsummary = s.Qrsummary == true ? true : false,
                Istreatment = s.Istreatment == true ? true : false,
                Articles = GSeoChannelLink.Count(),
                Hits = GIndhealClickAnalytics.Count(),
                Leads = GLeads.Count(),
                Enquiries = GEnquiry.Count(),
                Treatments = GTreatments.Count(),
                Hospitals = GHospitals.Count(),
                Cost = ph.Costusdmin + "-" + ph.Costusdmax

检查任何值是否为null,如果为null,则通过使用三元运算符分配一些非null值。例如 s.Seotreatmentid == null? 0:s.Seoprocessingid 。对所有值都执行此操作。

其中一个值是null,但可能是分配给了not null属性。