-2

Here is what I want to do : Get a List of Batch Objects in my controller that I can send to the view and display it. It's working but the values are mixed up. If the value in the database is null or 0, the query fills it with another non-null value in the record. Here's an example.

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

You can see that the TotalProcessed is not correct. Also if I have a CompletionDateTime that is not null in one of the objects, the List doesn't care and always outputs null

The code:

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

Batch Class(code first DB, so it's the definition of the DB as well)

    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

Here's the query from the 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}
Helpha
  • 452
  • 6
  • 17
  • 2
    Please post your Batch class and your context class. – Big Daddy Jan 13 '17 at 16:35
  • 2
    IQueryable is an interface, it doesn't fill anything. LINQ is just a query language it doesn't modify values either. Finally, LINQ to EF generates SQL statements, it doesn't modify the data by itself.  It's impossible to help without a) the class definitions b) the table schema and c) the affected rows. You may be looking at the wrong row, or the class's property may be replacing nulls, or the underlying query may be different than what you thought. – Panagiotis Kanavos Jan 13 '17 at 16:37
  • 1
    Check the generated SQL query eg with SQL Server Profiler. What happens when you execute the query against the database? – Panagiotis Kanavos Jan 13 '17 at 16:38
  • I edited my answer for what you asked, I'll try to track down the query – Helpha Jan 13 '17 at 16:42
  • 1
    I dont see where the problem emerges, but I would call your variable IBatchList as an I prefix normally means its an interface. – Bad Dub Jan 13 '17 at 16:59
  • Yeah, true, thanks for the input – Helpha Jan 13 '17 at 17:59

1 Answers1

0

I solved the problem, I don't know why it repairs it, but I added a where statement to the query(CreatedDateTime is never null by the way, so I'll alway return all my records) and now I have all the correct data.

 IEnumerable<BatchCode> IBatchCodeList = identityContext.BatchCodes.OrderByDescending(bc => bc.CreatedDateTime).Where(bc=>bc.CreatedDateTime != null);
Helpha
  • 452
  • 6
  • 17