I have a table in SQL Server 2008:
CREATE TABLE [dbo].[Pages](
[Id] [uniqueidentifier] NOT NULL,
[PageName] varchar(400) NOT NULL,
[Count] int NOT NULL,
CONSTRAINT [PK_Pages] PRIMARY KEY CLUSTERED
(
[Id] ASC
)) ON [PRIMARY]
And a unique index on that table:
CREATE UNIQUE NONCLUSTERED INDEX [IX_PageName] ON [dbo].[Pages]
(
[PageName] ASC
)
I get a bunch of page names from a text file which I group in Linq and count the occurrences.
var data = pages.GroupBy(x => x.PageName.ToLower().Trim())
.Select(x => new Page()
{
Id = Guid.NewGuid(),
PageName = x.Key,
Count = x.Count(),
})
.ToList();
I know that grouping in Linq to objects differs from Ling to Sql in that an IEnumerable grouping is case sensitive and an index in T-SQL ignores white space at the end of varchar. That's why I do grouping with ToLower()
and Trim()
above, but the index exception still fires. I have a lot of non-alpha carachters (forward slashes, backslashes, commas, dots etc) in the page names in the text file but I can't figure out which ones are causing the exception.
EDIT:
Here's the exception I get when debugging.
Cannot insert duplicate key row in object 'dbo.Pages' with unique index 'IX_PageName'.
The statement has been terminated.
SOLUTION:
Thanks to @Panagiotis Kanavos I solved it with the Sql profiler. A couple of the strings had asian letters in the middle of the string, e.g. 嘊 and my collation SQL_Latin1_General_CP1_CI_AS stored them as question marks. Still not sure this was a duplicate question since it had nothing to do with trailing blanks.