I have a Premiums table that I attempting to query using the following LINQ-to-SQL:
var premiums = sourcePolicyContext.Premiums.Where(prm => prm.Policy_Number == "07748106");
This runs the following SQL against the database:
exec sp_executesql N'SELECT [t0].[Policy Number] AS [Policy_Number], ' +
'[t0].[PremiiumType] AS [Premiium_Type], [t0].[Number], ' +
'[t0].[Effective Date] AS [Effective_Date], ' +
'[t0].[Entry Date] AS [Entry_Date], ' +
'[t0].[Collision Premium] AS [Collision_Premium], ' +
'[t0].[Non Collision Premium] AS [Non_Collision_Premium], ' +
'[t0].[Tow Premium] AS [Tow_Premium], ' +
'[t0].[Other Coverage1 Premium] AS [Other_Coverage1_Premium] ' +
'FROM [dbo].[Premium Table] AS [t0]' +
'WHERE [t0].[Policy Number] = @p0',
N'@p0 nvarchar(4000)',
@p0=N'07748106'
This query returns two rows when run directly, as expected. It also results in two LINQ to SQL entities, however the data in the two entities is just duplicates of the first row in the SQL query results. Why might this be happening?
Not sure if this will help, but if you haven’t already, try giving the table a dedicated unique identity column and set it as primary key. Make sure your LINQ-TO-SQL definitions know about the primary key column.