This is probably the shortest solution:
var grouped = MyList.GroupBy(f => f.GroupId != null ? (object)f.GroupId : new object(), key => new { Object = key });
Note that the "key" of the groups will be of object
type. For null
elements I create a new "empty" object
. The equality comparer of objects will make so that they are all different. For not-null numbers I simply box them in an object. Boxed integers maintain the equality operator. So:
new object().Equals(new object()) == false // always
and
((object)1).Equals((object)1) == true // always
and
((object)1).Equals((object)2) == false // always
a more correct solution would be implementing an IEqualityComparer<int?>
public class MyComparer : IEqualityComparer<int?> {
public bool Equals(int? x, int? y) {
if (x == null || y == null) {
return false;
}
return x.Value == y.Value;
}
public int GetHashCode(int? obj) {
return obj.GetHashCode(); // Works even if obj is null :-)
}
}
and using it:
var grouped2 = MyList.GroupBy(f => f.GroupId, key => new { Object = key }, new MyComparer());
GroupBy
maintains the order of the elements/groups, while your solution will destroy the ordering.