我正在C#中执行linq查询,所有操作都很顺利,但是当我在MVC中调用这个方法时它崩溃了,它不接受空值--我希望空值是“”
var referralDocs = (from d in _dc.ReferralDoctors
join mlog in _dc.LogPS.Where(x => x.id_action == 1)
on d.ReferralDoctorID equals mlog.id_PS into mlog
join _Category in _dc.ReferringDoctorCategories
on d.ReferringDoctorsCategory equals _Category.CategoryID into CatGroup
from _Category in CatGroup.DefaultIfEmpty()
where
d.DoctorID == doctorID && d.isDeleted == false && (d.NotificationEmail.Replace(" ", "").ToLower().Contains(search)
|| d.PracticePhone.Replace(" ", "").ToLower().Contains(search) || d.LastName.Replace(" ", "").ToLower().Contains(search) || d.FirstName.Replace(" ", "").ToLower().Contains(search))
select new ReferralDoctorInfo()
{
BirthDate = d.BirthDate,
DoctorID = d.DoctorID,
FirstName = d.FirstName != null ? d.FirstName.Trim().Replace("\r\n", "").Replace("\r", "").Replace("\n", "") : "",
HasOptOutNews = d.HasOptOutNews,
isDeleted = d.isDeleted,
LastName = d.LastName != null ? d.LastName.Trim().Replace("\r\n", "").Replace("\r", "").Replace("\n", "") : "",
NotificationEmail = d.NotificationEmail != null ? d.NotificationEmail.Trim().Replace("\r\n", "").Replace("\r", "").Replace("\n", "") : "",
PracticePhone = d.PracticePhone,
PracticeName = d.PracticeName != null ? d.PracticeName.Trim().Replace("\r\n", "").Replace("\r", "").Replace("\n", "") : "",
ReferralDoctorID = d.ReferralDoctorID,
lastOptOutDatetime = mlog.Where(x => x.client_id == doctorID && x.id_PS == d.ReferralDoctorID).Max(y => y.date),
DoctorCategory = _Category
});
好的,我在select: DoctorCategory = _Category中找到了这个
当我在MVC中得到这个对象时,这个对象的第一行拥有对象应该拥有的所有值,并且运行良好,但是第二行没有值,所以它给了我空值,这很好,因为我希望有那些没有类别但在这一点上崩溃的对象,因此我希望它们是“
我想要DefaultifEmpty或_Category中允许我更改空值的东西
像这样的东西
ReferingDoctorCategory = _Category.Name != null ? _Category.Name : "",
这样做的问题是不适用于对象,所以我无法使用它。
发布于 2015-08-26 22:07:38
如果有人想要和我一样的东西,我找到了答案
DoctorCategory = _Category ?? new ReferringDoctorCategory()
这并不会将空行更改为"“,但会将每个行都改为空行,这样MVC就不会崩溃。
https://stackoverflow.com/questions/32212904
复制相似问题