我有一个属性是,而不是 null (它是PK)。但是,我有一个查询,该查询在该表中使用左联接,创建了空值的可能性。
LINQ
(from a in dbContext.TableA
join b in dbContext.TableB on a.IdB equals b.IdB into bLeft
from bTbl in bLeft.DefaultIfEmpty()
select new
{
IdBTemp = bTbl.IdB, // This value may be null
IdATemp = a.IdA
}.AsEnumerable().Select(row => new DynamicClassModel()
{
// Stuff here (convertion to string, concatanations and other stuff)
});*类别模式*
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
[Column("IdB")]
public long IdB { get; set; }这只是一个小的查询示例。我的查询比这个更大,还有更多的左联接,还有更多的字段没有映射为null,但是在这个查询中它们可能是null。
我在阅读关于放任的文章时,还试图用一些值(比如0)填充空值。
如何将空值传递给非空属性?
发布于 2013-08-01 12:50:10
您可以尝试用默认值0填充它:
(from a in dbContext.TableA
join b in dbContext.TableB on a.IdB equals b.IdB into bLeft
from bTbl in bLeft.DefaultIfEmpty()
select new
{
IdBTemp = bTbl == null ? 0 : bTbl.IdB,
IdATemp = a.IdA
}.AsEnumerable().Select(row => new DynamicClassModel()
{
// Stuff here (convertion to string, concatanations and other stuff)
});但是,也许您希望使用int?代替。在这种情况下,请使用以下内容:
(from a in dbContext.TableA
join b in dbContext.TableB on a.IdB equals b.IdB into bLeft
from bTbl in bLeft.DefaultIfEmpty()
select new
{
IdBTemp = bTbl == null ? (int?)null : bTbl.IdB,
IdATemp = a.IdA
}.AsEnumerable().Select(row => new DynamicClassModel()
{
// Stuff here (convertion to string, concatanations and other stuff)
});发布于 2013-08-01 12:52:17
试着按您希望的那样指定默认值:bLeft.DefaultIfEmpty(0)或bLeft.DefaultIfEmpty(-1)
https://stackoverflow.com/questions/17994532
复制相似问题