views:

20

answers:

1

Hi,

I want to check for duplicates in Entity Framework using Linq-To-Entities, ignoring cases. What's the best way to do it? As far as I'm aware, context.[Entity].Contains(item, IEqualityComparer) method is not supported.

Do I have to do ToList() which reads the entire table into memory just to check it?

Thanks,

+1  A: 

To be honest the best place to look for duplicates is in the belly of the beast - the database. Much easier/faster.

However, if you must do it in the code, you could try StringComparer.OrdinalIgnoreCase:

StringComparer comparer = StringComparer.OrdinalIgnoreCase;
var caseInsensitiveResults = ctx.SomeObjectSet.Where(x => comparer.Equals(x.Field1, x.Field2));

As the .Equals method of the StringComparer object returns true/false, you can use it as the predicate.

Are you trying to look for duplicates in one table? Define "duplicate" in your specific scenario, might help us help you.

EDIT

Since your saying your looking for duplicate rows in a single table, the above won't work. It was just an example.

You'll need to use that comparer code in the GroupBy clause instead.

However, i still think you should do this in the database.

RPM1984
I am looking for duplicates as in "ABCD" "abcd" in a single table.
Kinderchocolate
Are you planning on "fixing" them in the code itself, or is this just a report?
RPM1984
It doesn't matter where I put as long as it works.
Kinderchocolate