sollniss
sollniss

Reputation: 2005

LINQ: How do I compare two elements?

I have a list of strings and if one element is a substring of another element, I want to remove that shorter element.

So,

{abc, def, ghi, ab, cd, ef} => {abc, def, ghi, cd}

I tried:

list = list.Where((x, y) => x.Item1 != y.Item1 && x.Item1.Contains(y.Item1) == false);

but somehow y is an integer.

Upvotes: 1

Views: 577

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

The overload of Where that you used is for filtering with an element and the index. You need to use the "regular" Where, like this:

var res = list.Where(x => !list.Any(y => x != y && y.Contains(x)));

Demo.

Upvotes: 1

Related Questions