[英]Unable to cast the type 'System.Nullable`1' to type 'System.Object' - ASP.NET MVC
[英]Left outer join gives error message 'Unable to cast the type 'System.Nullable`1' to type 'System.Object'.'
這真讓我發瘋!
我嘗試了多種方法(每種方法都進行了多次迭代),但是得到了相同的錯誤。
我已經在LINQPad中嘗試查詢,並獲得了所需的結果。
場景:一個碼頭。 如果船被注冊為停留在單據上,我想列出所有船單的清單。 如果沒有船憑單登記,那么boatID字段可以為NULL(我知道應該將其設置為鍵,但是我試圖使用Linq來獲取答案而不更改數據庫)。 有一個帶有清單清單的“清單”表,其中包括BoatId字段(用於在清單上注冊船只的時間)。 第二個表是“ Boat”表,以BoatId為鍵,並提供其他船詳細信息。
這是一個SQL查詢(產生我想要的結果):
Select s.SlipID, s.SlipNumber, s.Length, s.Electricity, s.Telephone, s.TV,
b.BoatName+' ['+b.BoatType+', '+convert(varchar,b.BoatOverAllLength)+']' as boatDets,
s.Status
from Slip as s left outer join boat as b on s.BoatID = b.BoatId;
這是產生錯誤的解決方案之一(但在LINQPad中有效):
var slipDets6 = from s6 in db.Slips
join b6 in db.Boats on s6.BoatId equals b6.BoatId into temp
from jn in temp.DefaultIfEmpty()
orderby s6.SlipNumber
select new
{
SlipID = (int?) s6.SlipId,
SlipNumber = s6.SlipNumber,
Length = s6.Length,
Electricity = s6.Electricity,
Telephone = s6.Telephone,
TV = s6.TV,
BoatDets = jn.BoatName + " [" + jn.BoatType + ", " + jn.BoatOverAllLength + "]",
Status = s6.Status
};
我收到的實際錯誤代碼是:
無法將類型System.Nullable'1
為類型System.Object
。 LINQ to Entities僅支持強制轉換EDM基本類型或枚舉類型。
我已經研究了在本網站(以及其他網站)上可以找到的盡可能多的解決方案,但是我似乎做對了。
1-在.netframework4中,您不能在linq中使用實體enumerations
,但是在.netframework4.5中,您可以使用enumeration
;在.netframework4中,當您使用enumeration
時,會發生此異常,盡管我在您的代碼中看不到任何enumeration
...
2-您可以在select
屬性中逐一注釋屬性,以查找導致異常的屬性,並檢查其類型...
3-如果在選擇數據作為literal
之前先獲取數據,則可能不會有任何問題,如下所示:
var slipDets6 = (from s6 in db.Slips
join b6 in db.Boats on s6.BoatId equals b6.BoatId into temp
from jn in temp.DefaultIfEmpty()
orderby s6.SlipNumber
select new {s6, jn})
.ToList()
.Select(u => new
{
SlipID = (int?)u.s6.SlipId,
SlipNumber = u.s6.SlipNumber,
Length = u.s6.Length,
Electricity = u.s6.Electricity,
Telephone = u.s6.Telephone,
TV = u.s6.TV,
BoatDets = u.jn.BoatName + " [" + u.jn.BoatType + ", " + u.jn.BoatOverAllLength + "]",
Status = u.s6.Status
})
.ToList();
4-請注意,如果jn
為null,則這行BoatDets = u.jn.BoatName + " [" + u.jn.BoatType + ", " + u.jn.BoatOverAllLength + "]",
引起異常
根據您的評論,問題是您嘗試從BoatName, BoatType and BoatOverAllLength
create a string
,您無法像我之前所說的那樣在linq to entities
BoatName, BoatType and BoatOverAllLength
字符串格式化linq to entities
(上一篇文章,no3),您可以獲取所需的數據並然后在內存中select
以創建BoatDets
字符串,因此這肯定可以工作:
var slipDets6 = (from s6 in db.Slips
join b6 in db.Boats on s6.BoatId equals b6.BoatId into temp
from jn in temp.DefaultIfEmpty()
orderby s6.SlipNumber
select new {s6, jn})
.ToList()
.Select(u => new
{
SlipID = (int?)u.s6.SlipId,
SlipNumber = u.s6.SlipNumber,
Length = u.s6.Length,
Electricity = u.s6.Electricity,
Telephone = u.s6.Telephone,
TV = u.s6.TV,
BoatDets = u.jn == null ? "" : u.jn.BoatName + " [" + u.jn.BoatType + ", " + u.jn.BoatOverAllLength + "]",
Status = u.s6.Status
})
.ToList();
或者,您可以獲取BoatName, BoatType and BoatOverAllLength
作為屬性,並在獲取查詢時從該屬性創建所需的字符串,如下所示:
public class FlatSlip
{
public int? SlipID { get; set; }
public string SlipNumber { get; set; }
public string Length { get; set; }
public string Electricity { get; set; }
public string Telephone { get; set; }
public string TV { get; set; }
public string BoatName { get; set; }
public string BoatType { get; set; }
public string BoatOverAllLength { get; set; }
public string Status { get; set; }
public string BoatDets
{
get
{
return this.BoatName + " [" + this.BoatType + ", " + this.BoatOverAllLength + "]";
}
}
}
var slipDets6 = from s6 in db.Slips
join b6 in db.Boats on s6.BoatId equals b6.BoatId into temp
from jn in temp.DefaultIfEmpty()
orderby s6.SlipNumber
select new FlatSlip()
{
SlipID = (int?)s6.SlipId,
SlipNumber = s6.SlipNumber,
Length = s6.Length,
Electricity = s6.Electricity,
Telephone = s6.Telephone,
TV = s6.TV,
BoatName = jn == null ? "" : jn.BoatName,
BoatType = jn == null ? "" : jn.BoatType,
BoatOverAllLength = jn == null ? "" : jn.BoatOverAllLength,
Status = s6.Status
};
或者如果您堅持使用literal
:
public class Boat
{
public Boat()
{
}
public Boat(string BoatName, string BoatType, string BoatOverAllLength)
{
this.BoatName = BoatName;
this.BoatType = BoatType;
this.BoatOverAllLength = BoatOverAllLength;
}
public string BoatName { get; set; }
public string BoatType { get; set; }
public string BoatOverAllLength { get; set; }
public string BoatDets
{
get
{
return this.BoatName + " [" + this.BoatType + ", " + this.BoatOverAllLength + "]";
}
}
}
var slipDets6 = (from s6 in db.Slips
join b6 in db.Boats on s6.BoatId equals b6.BoatId into temp
from jn in temp.DefaultIfEmpty()
orderby s6.SlipNumber
select new { s6, jn })
.ToList()
.Select(u => new
{
SlipID = (int?)u.s6.SlipId,
SlipNumber = u.s6.SlipNumber,
Length = u.s6.Length,
Electricity = u.s6.Electricity,
Telephone = u.s6.Telephone,
TV = u.s6.TV,
BoatDets = jn == null ? new Boat() : new Boat(u.jn.BoatName, u.jn.BoatType, u.jn.BoatOverAllLength),
Status = u.s6.Status
})
.ToList();
注意:在我的最后兩個查詢中, BoatDets
屬性不能在linq to entity
BoatDets
,並且在將數據提取到內存時可讀
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.