Andis59
Andis59

Reputation: 569

Query always returns all records

I have this query

_context.LaserData
    .OrderBy(x => x.F1).ThenBy(x => x.Kant)
    .Where(x => x.Avdelning.StartsWith("AME"))
    .Load();

    result = _context.LaserData.Local;

The query will create the following SQL (I'm using _context.Database.Log = s => System.Diagnostics.Debug.WriteLine(s); to get the SQL)

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[F1] AS [F1], 
    [Extent1].[Kant] AS [Kant], 
    [Extent1].[Avdelning] AS [Avdelning], 
    [Extent1].[F2] AS [F2], 
    [Extent1].[F3] AS [F3], 
    [Extent1].[F4] AS [F4], 
    [Extent1].[F5] AS [F5], 
    [Extent1].[F6] AS [F6], 
    [Extent1].[F7] AS [F7], 
    [Extent1].[F8] AS [F8], 
    [Extent1].[F9] AS [F9], 
    [Extent1].[F10] AS [F10], 
    [Extent1].[BC1] AS [BC1], 
    [Extent1].[BC2] AS [BC2], 
    [Extent1].[Template] AS [Template], 
    [Extent1].[P1] AS [P1], 
    [Extent1].[P2] AS [P2], 
    [Extent1].[P3] AS [P3], 
    [Extent1].[P4] AS [P4], 
    [Extent1].[P5] AS [P5], 
    [Extent1].[P6] AS [P6], 
    [Extent1].[FixtureId] AS [FixtureId], 
    [Extent1].[ExternTest] AS [ExternTest], 
    [Extent1].[EnableTO] AS [EnableTO], 
    [Extent1].[TOnr] AS [TOnr]
    FROM [dbo].[LaserData] AS [Extent1]
    WHERE [Extent1].[Avdelning] LIKE N'AME%'
    ORDER BY [Extent1].[F1] ASC, [Extent1].[Kant] ASC

If I run the SQL query in Microsoft SQL Server Management Studio I get 4 records.

If I run the query in my program I get 1823 records, which is all the records in the table.

Any one that can see what I'm doing wrong?

EDIT: The query is used to populate an ObservableCollection that is bound to an DataGrid.

Upvotes: 0

Views: 71

Answers (1)

DavidG
DavidG

Reputation: 119017

My guess is that your context already has the LaserData items tracked locally from a previous query. Instead, you should just query the database directly:

result = _context.LaserData
    .OrderBy(x => x.F1).ThenBy(x => x.Kant)
    .Where(x => x.Avdelning.StartsWith("AME"))
    .ToList();

Upvotes: 2

Related Questions