[英]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.