Add NotNull for IEnumerable<T> where T : struct

This commit is contained in:
erri120 2021-01-09 19:44:29 +01:00
parent 019bd9282b
commit a5088bc10e

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Wabbajack
{
@ -37,14 +38,21 @@ namespace Wabbajack
/// <summary>
/// Converts and filters a nullable enumerable to a non-nullable enumerable
/// </summary>
public static IEnumerable<T> NotNull<T>(this IEnumerable<T?> e)
public static IEnumerable<T> NotNull<T>(this IEnumerable<T?> enumerable)
where T : class
{
// Filter out nulls
return e.Where(e => e != null)
return enumerable.Where(e => e != null)
// Cast to non nullable type
.Select(e => e!);
}
public static IEnumerable<T> NotNull<T>(this IEnumerable<T?> enumerable) where T : struct
{
return enumerable
.Where(x => x.HasValue)
.Select(x => x!.Value);
}
/// <summary>
/// Selects items that are castable to the desired type