Last active
April 20, 2023 10:50
-
-
Save smdooley/d28b1552fec1c29bbf037b6c4a56c0ad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Umbraco.Core.Models.PublishedContent; | |
using Umbraco.Web; | |
public static class EnumerableExtensions { | |
public static bool HasAny<T> (this IEnumerable<T> items) => items != null && items.Any(); | |
public static bool IsNullOrEmpty<T>(this IEnumerable<T> items) | |
{ | |
return items == null && !items.Any(); | |
} | |
public static T FirstOrDefaultOfType<T> (this IEnumerable<IPublishedContent> publishedContents) => publishedContents.OfType<T> ().FirstOrDefault(); | |
public static string JoinIfNotNull <TResult> (this IEnumerable <IPublishedContent> publishedContents, Func <IPublishedContent, TResult> func, string separator = StringConstants.Separators.Space) => !publishedContents.HasValue() ? | |
string.Empty : | |
string.Join(separator, publishedContents.Select(func)); | |
public static IEnumerable <(T item, int index)> WithIndex<T> (this IEnumerable <T> items) => items.HasAny() ? | |
items | |
.Select((item, index) => (item, index)) | |
.ToList() : | |
Enumerable.Empty < (T item, int index) > (); | |
public static IEnumerable<T> OrderBySequence <T, TId> (this IEnumerable<T> source, | |
IEnumerable <TId> order, | |
Func <T, TId> idSelector) { | |
var lookup = source.ToDictionary(idSelector, t => t); | |
foreach(var id in order) { | |
yield | |
return lookup[id]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment