public static IEnumerable <XObject> DescendantObjects(this XObject source) { source.NotNull(nameof(source)); return(Enumerable .Empty <XObject>() .Concat( (source as XElement)?.Attributes() // T is covariant in IEnumerable<T>. ?? Enumerable.Empty <XObject>()) .Concat( (source as XContainer)? .DescendantNodes() .SelectMany(descendant => Enumerable .Repeat <XObject>(descendant, 1) .Concat( (descendant as XElement)?.Attributes() // T is covariant in IEnumerable<T>. ?? Enumerable.Empty <XObject>())) ?? Enumerable.Empty <XObject>())); }