[英]LINQ query Joins
當我使用以下代碼檢索信息時,它顯示錯誤。
var mails = from m in entity.mailboxes
join p in entity.ordinary_user_profile_info on m.from_id equals p.user_id
select new MailList
{
mid = m.m_id,
mfrom = m.**from_id,** // Error occours here
mfomname = p.username,
msubject = m.subject
};
錯誤是:
“ int?mailbox.from_id”
無法隱式轉換類型'int?' 到“ int”。 存在顯式轉換(您是否缺少演員表?)
我已經在數據庫以及MailList類m_id
和from_id
聲明為int。
我猜這應該解決它。
如此詮釋? 是一個空類型 ,你需要或者
(1)將MailList.mfrom
定義為int
嗎? 要么
(2)從int轉換? 轉換為int,如下所示 :
var mails = from m in entity.mailboxes
join p in entity.ordinary_user_profile_info on m.from_id equals p.user_id
select new MailList
{
mid = m.m_id,
**mfrom = m.from_id.HasValue ? m.from_id.Value : 0**
//this is saying is, since the int is nullable,
//if it has a value, take it, or return 0
mfomname = p.username,
msubject = m.subject
};
更新資料
經過更多的研究,根據msdn的說法 ,使用null運算符的 @abatishchev解決方案似乎是正確的方法,並且像@Konstantin提到的Nullable.GetValueOrDefault(T)上的注釋也更正確。
var mails = from m in entity.mailboxes
join p in entity.ordinary_user_profile_info on m.from_id equals p.user_id
select new MailList
{
mid = m.m_id,
mfrom = m.from_id ?? 0,
mfomname = p.username,
msubject = m.subject
};
嘗試這個:
var mails = from m in entity.mailboxes
join p in entity.ordinary_user_profile_info on m.from_id equals p.user_id
select new MailList
{
mid = m.m_id,
mfrom = (int)m.from_id, // Error occours here
mfomname = p.username,
msubject = m.subject
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.