Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to SQL query returning duplicates

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?

like image 397
Starr Avatar asked Jul 16 '26 16:07

Starr


1 Answers

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.

like image 105
Pieter Müller Avatar answered Jul 19 '26 05:07

Pieter Müller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!