IQueryable 用其他值填充空值

IQueryable fills null values with other values

这是我想要做的:在我的控制器中获取一个批处理对象列表,我可以将其发送到视图并显示它。它正在工作,但价值观混淆了。如果数据库中的值为 null 或 0,查询将用记录中的另一个非空值填充它。这是一个例子。

Database Content
    Id:13
    TotalRequest : 10
    TotalProcessed :0
    CreatedDateTime:2017-01-13 13:30:46.090
    CreatedBy:Test
    CompletionDateTime : NULL

Iqueryable at position 13 content
        Id:13
        TotalRequest : 10
        TotalProcessed :10
        CreatedDateTime:2017-01-13 13:30:46.090
        CreatedBy:Test
        CompletionDateTime : NULL

可以看到TotalProcessed不正确。此外,如果我的 CompletionDateTime 在其中一个对象中不为 null,则 List 并不关心并且始终输出 null

代码:

            IQueryable<Batch> IBatchList = context.batch.OrderByDescending(b => b.CreatedDateTime);
        var batchList = IBatchList.ToList();

批处理Class(代码优先数据库,所以它也是数据库的定义)

    public class Batch
{
    [Key]
    public Int64 Id { get; set; }
    public int TotalRequested { get; set; }
    public int TotalProcessed { get; set; }
    public DateTime CreatedDateTime { get; set; }
    public string CreatedBy { get; set; }
    public DateTime? CompletedDateTime { get; set; }
}

Id  TotalRequested  TotalProcessed  CreatedDateTime CreatedBy   CompletedDateTime
13  10  0   2017-01-13 13:30:46.090 Test    NULL

这是来自 Iqueryable 的查询:

{SELECT 
[Extent1].[Id] AS [Id], 
[Extent1].[TotalRequested] AS [TotalRequested], 
[Extent1].[TotalProcessed] AS [TotalProcessed], 
[Extent1].[CreatedDateTime] AS [CreatedDateTime], 
[Extent1].[CreatedBy] AS [CreatedBy], 
[Extent1].[CompletedDateTime] AS [CompletedDateTime]
FROM [dbo].[Batch] AS [Extent1]
ORDER BY [Extent1].[CreatedDateTime] DESC}

我解决了这个问题,我不知道它为什么会修复它,但是我在查询中添加了一个 where 语句(顺便说一下,CreatedDateTime 永远不会为 null,所以我总是 return 我所有的记录),现在我有了所有正确的数据。

 IEnumerable<BatchCode> IBatchCodeList = identityContext.BatchCodes.OrderByDescending(bc => bc.CreatedDateTime).Where(bc=>bc.CreatedDateTime != null);