If the order of the elements does not matter, then it is a matter of sorting your list from the longest to the shortest, and then providing a custom equality mapping to the Distinct LINQ method.
The comparator implements both GetHashCode and Equals . Since Equals will not be called if the hash codes are not equal, you can just take it out, always returning 0 . The rules for GetHashCode indicate that things that are not equal can return the same hash code so that you don't violate the semantics here.
Then the Equals method is simply compared to see if the old line starts with a new line. The new line is passed as the first argument, and the old line is passed as the second argument.
Then our comparator looks like this:
public class StartsWithEqualityComparer : IEqualityComparer<String> {
Then you can make the call using it using the Distinct method
var foo = list.OrderByDescending(s=> s.Count()) .Distinct (new StartsWithEqualityComparer ()) .ToList();
Finally, if necessary, you can use the Sort method to reorder the list in the desired order (eq in alphabetical order).
source share