我想以不同的方式阻止这种情况发生,而不是将DateCreated的类型设置为可为空的DateTime。
完整的例外内容如下:
转换为值类型' System.DateTime'失败,因为 具体化值为null。结果类型的通用参数 或者查询必须使用可空类型。
当我在数据库上运行以下查询时,我可以看到那里没有空记录,所以我相信我应该避免将DateCreate属性设置为可以为空的DateTime,因为它对我来说没有多大意义(即使我知道它会解决问题)。有什么我理解错了吗?
查询的以下行导致问题:
DateCreated = subJoined.dateUploaded
这是完整的LINQ查询:
using (var db = new ABEntities())
{
var features = (from textObject in db.textObjects
join container in db.containers.DefaultIfEmpty() on textObject.textObjectPK equals
container.textObjectPK into tObjsContainerJoined
from subContainerJoin in tObjsContainerJoined.DefaultIfEmpty()
join tObjsMedia in db.media on subContainerJoin.mediaID equals
tObjsMedia.mediaID into tObjsMediaJoined
from subJoined in tObjsMediaJoined.DefaultIfEmpty()
from textContainer in tObjsContainerJoined
where
textObject.version == Constants.Versions.LATEST &&
textObject.textObjectTypeID == Constants.News.FEATURES &&
textObject.deployDate <= DateTime.Now
select new TextObject
{
Id = textObject.textObjectID,
Title = textObject.title,
ContainerId = textContainer.containerID,
Description = textContainer.container1,
DateCreated = textObject.deployDate,
Media = new Media
{
Title = subJoined.title,
MediaFormat = subJoined.extension,
MediaTypeID = subJoined.mediaTypeID,
MediaFile = subJoined.fileName,
Credit = subJoined.credit,
MembersOnly = subJoined.membersOnly,
LastModified = subJoined.lastModified,
DateCreated = subJoined.dateUploaded
},
TypeId = textObject.textObjectTypeID
}).OrderByDescending(t => t.DateCreated).ToList();
return features;
}
这是媒体类定义:
[Serializable]
public class Media
{
public int Id { get; set; }
public string MediaFile { get; set; }
public string Title { get; set; }
public string Credit { get; set; }
public int? MediaTypeID { get; set; }
public string MediaFormat { get; set; }
public bool? isYoutube { get; set; }
public string YoutubeID { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public int Views { get; set; }
public string Description { get; set; }
public int SiloID { get; set; }
public DateTime DateCreated { get; set; }
public bool IsVideo { get; set; }
public int SegmentId { get; set; }
public string Extension { get; set; }
public bool? ShowOnHomepage { get; set; }
public bool? MembersOnly { get; set; }
public DateTime? LastModified { get; set; }
}
正在生成的查询 - 由SQL事件探查器捕获:
SELECT
[Project1].[textObjectPK] AS [textObjectPK],
[Project1].[textObjectID] AS [textObjectID],
[Project1].[title] AS [title],
[Project1].[containerID] AS [containerID],
[Project1].[container] AS [container],
[Project1].[deployDate] AS [deployDate],
[Project1].[title1] AS [title1],
[Project1].[extension] AS [extension],
[Project1].[mediaTypeID] AS [mediaTypeID],
[Project1].[fileName] AS [fileName],
[Project1].[credit] AS [credit],
[Project1].[membersOnly] AS [membersOnly],
[Project1].[C1] AS [C1],
[Project1].[dateUploaded] AS [dateUploaded],
[Project1].[textObjectTypeID] AS [textObjectTypeID]
FROM ( SELECT
[Extent1].[textObjectPK] AS [textObjectPK],
[Extent1].[textObjectID] AS [textObjectID],
[Extent1].[textObjectTypeID] AS [textObjectTypeID],
[Extent1].[title] AS [title],
[Extent1].[deployDate] AS [deployDate],
CAST( [Extent3].[lastModified] AS datetime2) AS [C1],
[Extent3].[mediaTypeID] AS [mediaTypeID],
[Extent3].[fileName] AS [fileName],
[Extent3].[title] AS [title1],
[Extent3].[extension] AS [extension],
[Extent3].[credit] AS [credit],
[Extent3].[dateUploaded] AS [dateUploaded],
[Extent3].[membersOnly] AS [membersOnly],
[Join4].[containerID] AS [containerID],
[Join4].[container] AS [container]
FROM [dbo].[textObjects] AS [Extent1]
LEFT OUTER JOIN (SELECT [Extent2].[textObjectPK] AS [textObjectPK], [Extent2].[mediaID] AS [mediaID]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
INNER JOIN [dbo].[containers] AS [Extent2] ON 1 = 1 ) AS [Join1] ON [Extent1].[textObjectPK] = [Join1].[textObjectPK]
LEFT OUTER JOIN [dbo].[media] AS [Extent3] ON [Join1].[mediaID] = [Extent3].[mediaID]
INNER JOIN (SELECT [Extent4].[containerID] AS [containerID], [Extent4].[textObjectPK] AS [textObjectPK], [Extent4].[container] AS [container]
FROM ( SELECT 1 AS X ) AS [SingleRowTable2]
INNER JOIN [dbo].[containers] AS [Extent4] ON 1 = 1 ) AS [Join4] ON [Extent1].[textObjectPK] = [Join4].[textObjectPK]
WHERE (1 = [Extent1].[version]) AND (2 = [Extent1].[textObjectTypeID]) AND ([Extent1].[deployDate] <= (SysDateTime()))
) AS [Project1]
ORDER BY [Project1].[deployDate] DESC
答案 0 :(得分:11)
您可以将查询中的值转换为可为空的类型,并检查它是否为Nullable
,而不是将数据类型设置为null
。
DateCreated = (DateTime?)subJoined.dateUploaded ?? DateTime.Now
您的查询将如下所示:
using (var db = new ABEntities())
{
var features = (from textObject in db.textObjects
join container in db.containers.DefaultIfEmpty() on textObject.textObjectPK equals container.textObjectPK into tObjsContainerJoined
from subContainerJoin in tObjsContainerJoined.DefaultIfEmpty()
join tObjsMedia in db.media on subContainerJoin.mediaID equals tObjsMedia.mediaID into tObjsMediaJoined
from subJoined in tObjsMediaJoined.DefaultIfEmpty()
from textContainer in tObjsContainerJoined
where textObject.version == Constants.Versions.LATEST &&
textObject.textObjectTypeID == Constants.News.FEATURES && textObject.deployDate <= DateTime.Now
select new TextObject
{
Id = textObject.textObjectID,
Title = textObject.title,
ContainerId = textContainer.containerID,
Description = textContainer.container1,
DateCreated = textObject.deployDate,
Media = new Media
{
Title = subJoined.title,
MediaFormat = subJoined.extension,
MediaTypeID = subJoined.mediaTypeID,
MediaFile = subJoined.fileName,
Credit = subJoined.credit,
MembersOnly = subJoined.membersOnly,
LastModified = subJoined.lastModified,
DateCreated = (DateTime?)subJoined.dateUploaded ?? DateTime.Now
},
TypeId = textObject.textObjectTypeID
}).OrderByDescending(t => t.DateCreated).ToList();
return features;
}
为什么它会在该特定行上出错?
免责声明:这只是猜测(如果我错了,请纠正我......)
可能是因为你做左连接而且EF期望某些值可以为空并且它会引发错误作为预防措施。它只会在DateTime
数据类型上抛出一个错误,因为它知道当其他属性为null(字符串和可空类型)时该怎么做。