You cannot group by a list, even an ordered one, but you can group by a single string produced by concatenating the strings in a specific order. Assuming that there is a character that never appears in any of the strings (say, '|'
), you could do it like this:
objList = objList
.GroupBy(x => string.Join("|", x.StrList.OrderBy(s => s)))
.Select(g => g.OrderByDescending(x => x.Num).First())
.ToList();
The idea is to construct a grouping key from individual strings by concatenating them in the same order (ascending lexicographical).
This should work fine for small amounts of data. If your lists are extremely large, you could implement a special class to use as a multi-part key for grouping without making potentially large keys:
public class MultipartKey : IEquitable<MultipartKey> {
private IList<string> parts;
int hashCode;
public MultipartKey(IEnumerable<string> parts) {
this.parts = parts.OrderBy(s => s).ToList();
hashCode = this.parts.Aggregate(0, (p, v) => 31*p + v.GetHashCode());
}
public override int GetHashCode() {
return hashCode;
}
public override bool Equals(MultipartKey other) {
return parts.SequenceEqual(other.Parts);
}
}
and using it in a query like this:
objList = objList
.GroupBy(x => new MultipartKey(x.StrList))
.Select(g => g.OrderByDescending(x => x.Num).First())
.ToList();