Data extraction, strange behavior

I am using EF 6 to retrieve some data from SQL Server. I have encountered this problem in the past, but I have not asked anyone about this.

For the code below, just focus on the selection.

Let's say in the database I have 2 lines:

1 2 3 4  5 
6 7 8 9 10

My code is:

 var results = db.Table.Where(o => o.Version == 1)
                       .Select(o => new List<double?>{ o.M01, o.M02, o.M03, o.M04, o.M05});

 return results.ToList();

The above code will return a list of lists with the following:

previousDemand[0] = 1 2 3 4 5
previousDemand[1] = 10 9 8 7 6

The second list is canceled.

If I have more lines, this is one and the same: first normal, second vice versa, third normal, fourth, etc.

If I change the code to this:

 var results = db.Table.Where(o => o.Version == 1).ToList();
 var x = results.Select(o => new List<double?>{ o.M01, o.M02, o.M03, o.M04, o.M05});

 return x.ToList();

everything will work well.

If it seems that there is a problem, if I create lists in an EF request.

Did I miss something important?

UPDATE Adding a few screenshots: Sql Server

What do I get in EF

+4
source share
2 answers

, EF List<T> , . , SQL . , EF SQL-, . SQL - , , . SQL, , , EF ( ) .

EF, , . , .

IMO - . M records --each, , , - .

+2

TL; DR

new List<double?> { o.M01, o.M02, o.M03, o.M04, o.M05 }.OrderBy(p => p).ToList()

sugestion , , , @GertArnold .

, . .

var results = 
   db.Entities.Where(o => o.Version == 1)
     .Select(o => new { M01 = o.M01, M02 = o.M02, M03 = o.M03, M04 = o.M04, M05 = o.M05 });

return results.Select(o => new List<double?> { o.M01, o.M02, o.M03, o.M04, o.M05 });

:

EF ( ) SQL:

SELECT 
    [Project6].[Id] AS [Id], 
    [Project6].[C2] AS [C1], 
    [Project6].[C1] AS [C2]
    FROM ( 
        SELECT 
            CASE 
                WHEN ([UnionAll4].[C1] = 0) THEN [Extent1].[M01] 
                WHEN ([UnionAll4].[C1] = 1) THEN [Extent1].[M02] 
                WHEN ([UnionAll4].[C1] = 2) THEN [Extent1].[M03] 
                WHEN ([UnionAll4].[C1] = 3) THEN [Extent1].[M04] 
                ELSE [Extent1].[M05] END AS [C1], 
            [Extent1].[Id] AS [Id], 
            1 AS [C2]
        FROM  [dbo].[Entities] AS [Extent1]
        CROSS JOIN  (
            SELECT 0 AS [C1]
            FROM  ( SELECT 1 AS X ) AS [SingleRowTable1]                
            UNION ALL               
                SELECT 1 AS [C1]
                FROM  ( SELECT 1 AS X ) AS [SingleRowTable2]                
            UNION ALL               
                SELECT 2 AS [C1]
                FROM ( SELECT 1 AS X ) AS [SingleRowTable3]             
            UNION ALL               
                SELECT 3 AS [C1]
                FROM  ( SELECT 1 AS X ) AS [SingleRowTable4]                
            UNION ALL               
                SELECT 4 AS [C1]
                FROM  ( SELECT 1 AS X ) AS [SingleRowTable5]
            ) AS [UnionAll4]
            WHERE 1 = [Extent1].[Version]
    )  AS [Project6]
ORDER BY [Project6].[Id] ASC, [Project6].[C2] ASC

ORDER BY, [Project6]. [C2], , 1. , , @marc_s, .

somenting, :

< >   var results =      db.Entities.Where(o = > o.Version == 1)        . (o = > {o.M01, o.M02, o.M03, o.M04, o.M05}.OrderBy(p = > p).ToList());

... SQL , exepcted. >

var results = 
   db.Entities.Where(o => o.Version == 1)
     .Select(o => new { M01 = o.M01, M02 = o.M02, M03 = o.M03, M04 = o.M04, M05 = o.M05 });

return results.Select(o => new List<double?> { o.M01, o.M02, o.M03, o.M04, o.M05 });

... SQL , .

+4

Source: https://habr.com/ru/post/1688017/


All Articles