how to find duplicate in a string using LINQ

Posted by Klbaiju under ASP.NET on 6/25/2013 | Points: 10 | Views : 1044 | Status : [Member] | Replies : 1
Hi,
I want to find the duplicate in a string using LINQ.

eg i want to display distnct values of "WELECOME".
output should be "WELCOME"

how it is possible.

Regards
Baiju




Responses

Posted by: Allemahesh on: 6/25/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
You can use the below code:-

List<string> items = strTry.Select(c => c.ToString()).ToList();
var distinctItems = items.Distinct();

To match on only some of the properties, create a custom equality comparer, e.g.:
class DistinctItemComparer : IEqualityComparer<Item> {

public bool Equals(Item x, Item y) {
return x.Id == y.Id &&
x.Name == y.Name &&
x.Code == y.Code &&
x.Price == y.Price;
}

public int GetHashCode(Item obj) {
return obj.Id.GetHashCode() ^
obj.Name.GetHashCode() ^
obj.Code.GetHashCode() ^
obj.Price.GetHashCode();
}
}
Then use it like this:
var distinctItems = items.Distinct(new DistinctItemComparer());

Hope this will help you.

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response