开发者

Entity framework, check duplicates ignoring case

开发者 https://www.devze.com 2023-01-22 02:59 出处:网络
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, IEquali

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,


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消