[英]Left outer join gives error message 'Unable to cast the type 'System.Nullable`1' to type 'System.Object'.'
This is driving me nuts! 这真让我发疯!
I have tried a number of ways (and many iterations of each way) but get the same error. 我尝试了多种方法(每种方法都进行了多次迭代),但是得到了相同的错误。
I have tried the query in LINQPad and get the desired result. 我已经在LINQPad中尝试查询,并获得了所需的结果。
Scenario: A marina. 场景:一个码头。 I want a list of all the slips with boat details, if a boat is registered as staying at the slip.
如果船被注册为停留在单据上,我想列出所有船单的清单。 If there is no boat registered at a slip then the boatID field could be NULL (I know this should be set up as a key but I am trying to use Linq to get to the answer without changing the data base).
如果没有船凭单登记,那么boatID字段可以为NULL(我知道应该将其设置为键,但是我试图使用Linq来获取答案而不更改数据库)。 There is a 'Slip' table with a list of slips, including a BoatId field (for when a boat is registered at the slip).
有一个带有清单清单的“清单”表,其中包括BoatId字段(用于在清单上注册船只的时间)。 The second table is a 'Boat' table, with BoatId as the key and other boat details.
第二个表是“ Boat”表,以BoatId为键,并提供其他船详细信息。
Here is a SQL Query (that produces the result I want): 这是一个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;
Here is one of the solutions that gives an error (but works in LINQPad): 这是产生错误的解决方案之一(但在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
};
The actual error code I receive is: 我收到的实际错误代码是:
Unable to cast the type System.Nullable'1
to type System.Object
. 无法将类型
System.Nullable'1
为类型System.Object
。 LINQ to Entities only supports casting EDM primitive or enumeration types. LINQ to Entities仅支持强制转换EDM基本类型或枚举类型。
I have delved into as many solutions I could find on this site (and others) but I seem to be doing the right thing. 我已经研究了在本网站(以及其他网站)上可以找到的尽可能多的解决方案,但是我似乎做对了。
1- in .netframework4 you cant use enumerations
in linq to entities, but in .netframework4.5 you can, and in .netframework4 when you use enumeration
this exception occurs, although i dont see any enumeration
in your codes... 1-在.netframework4中,您不能在linq中使用实体
enumerations
,但是在.netframework4.5中,您可以使用enumeration
;在.netframework4中,当您使用enumeration
时,会发生此异常,尽管我在您的代码中看不到任何enumeration
...
2- you can comment properties in your select
one by one, to find the property causing exception, and check its type... 2-您可以在
select
属性中逐一注释属性,以查找导致异常的属性,并检查其类型...
3- probably you will not any problem, if fetch data before select them as a literal
like this: 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- be careful, if jn
be null, this line BoatDets = u.jn.BoatName + " [" + u.jn.BoatType + ", " + u.jn.BoatOverAllLength + "]",
is causing an exception 4-请注意,如果
jn
为null,则这行BoatDets = u.jn.BoatName + " [" + u.jn.BoatType + ", " + u.jn.BoatOverAllLength + "]",
引起异常
according to your comment, the problem is where u try to create a string
from BoatName, BoatType and BoatOverAllLength
, you cant format a string in linq to entities
, as i said before (prev post, no3), you can fetch data you need and then select
in memory to create BoatDets
string, so this, surely works: 根据您的评论,问题是您尝试从
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();
or, you can fetch BoatName, BoatType and BoatOverAllLength
as property, and when your query fetched, create string you need from that properties, something like this: 或者,您可以获取
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
};
or if you are insisting on using literal
: 或者如果您坚持使用
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();
notice: BoatDets
property, in my two last queries, can not be used in linq to entity
, and its readable when your data has been fetched to memory 注意:在我的最后两个查询中,
BoatDets
属性不能在linq to entity
BoatDets
,并且在将数据提取到内存时可读
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.