CSharpFunctionalExtensions
CSharpFunctionalExtensions copied to clipboard
IEnumerable<Result<T>> to Result<IEnumerable<T>>
For anyone interested:
public static class ResultExtensions
{
public static bool IsFailure<T>(this IEnumerable<Result<T>> source) => source.Any(result => result.IsFailure);
public static bool IsSuccess<T>(this IEnumerable<Result<T>> source) => !source.IsFailure();
public static Result<IEnumerable<T>> ToResult<T>(this IEnumerable<Result<T>> source)
{
var sourceList = source.ToList();
return sourceList.IsFailure()
? Result.Fail<IEnumerable<T>>(sourceList.Error())
: Result.Ok(sourceList.Select(result => result.Value));
}
public static string Error<T>(this IEnumerable<Result<T>> source)
{
var sourceList = source.ToList();
return sourceList.IsSuccess()
? default
: sourceList
.Where(result => result.IsFailure)
.Select(result => result.Error)
.Aggregate(new StringBuilder(), (stringBuilder, error) => stringBuilder.AppendLine(error))
.ToString();
}
}
Hi @FrancoisM I think It method already exists: public static Result<IEnumerable<T>> Combine<T>(this IEnumerable<Result<T>> results, string errorMessagesSeparator)
@ahryshchanka
I am trying to use that method, but I don't see that method signature on the Result struct in the latest version, 2.2.2.0, on NuGet. Has that feature not been released yet?
Edit:
Just realized the Combine method I was looking for was an IEnumerable<Result<T>> extension method, not a Result extension method.