C# IQueryable使用其他值填充空值
下面是我想要做的:在我的控制器中获取一个批处理对象列表,我可以将其发送到视图并显示它。它起作用了,但价值观混淆了。如果数据库中的值为null或0,则查询将使用记录中的另一个非null值填充它。这里有一个例子C# IQueryable使用其他值填充空值,c#,asp.net-mvc,linq,iqueryable,C#,Asp.net Mvc,Linq,Iqueryable,下面是我想要做的:在我的控制器中获取一个批处理对象列表,我可以将其发送到视图并显示它。它起作用了,但价值观混淆了。如果数据库中的值为null或0,则查询将使用记录中的另一个非null值填充它。这里有一个例子 Database Content Id:13 TotalRequest : 10 TotalProcessed :0 CreatedDateTime:2017-01-13 13:30:46.090 CreatedBy:Test Completi
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,则列表并不关心,并且总是输出null
守则:
IQueryable<Batch> IBatchList = context.batch.OrderByDescending(b => b.CreatedDateTime);
var batchList = IBatchList.ToList();
以下是来自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,所以我将始终返回所有记录),现在我有了所有正确的数据
IEnumerable<BatchCode> IBatchCodeList = identityContext.BatchCodes.OrderByDescending(bc => bc.CreatedDateTime).Where(bc=>bc.CreatedDateTime != null);
IEnumerable IBatchCodeList=identityContext.BatchCodes.OrderByDescending(bc=>bc.CreatedDateTime)。其中(bc=>bc.CreatedDateTime!=null);
请发布批处理类和上下文类。IQueryable是一个接口,它不填充任何内容。LINQ只是一种查询语言,它也不修改值。最后,LINQtoEF生成SQL语句,它本身不修改数据。如果没有a)类定义b)表模式和c)受影响的行,就无法提供帮助。您可能看到错误的行,或者类的属性可能正在替换null,或者基础查询可能与您所想的不同。请使用SQL Server Profiler检查生成的SQL查询,例如。当您对数据库执行查询时会发生什么情况?我根据您的提问编辑了我的答案,我将尝试跟踪查询。我不知道问题出在哪里,但我会调用您的变量IBatchList,因为I前缀通常意味着它是一个接口。
IEnumerable<BatchCode> IBatchCodeList = identityContext.BatchCodes.OrderByDescending(bc => bc.CreatedDateTime).Where(bc=>bc.CreatedDateTime != null);