-
Notifications
You must be signed in to change notification settings - Fork 0
/
SingleOrNoneAsync.cs
31 lines (28 loc) · 1.62 KB
/
SingleOrNoneAsync.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System.Linq.Expressions;
using Funcky.Monads;
using Microsoft.EntityFrameworkCore;
namespace Funcky.Extensions;
public static partial class EntityFrameworkQueryableExtensions
{
/// <summary>
/// Returns the only element of a sequence as an <see cref="Option" />, or a <see cref="Option{T}.None" /> value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.
/// </summary>
/// <typeparam name="TSource">the inner type of the queryable.</typeparam>
public static async Task<Option<TSource>> SingleOrNoneAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
where TSource : notnull
=> await source
.Select(x => Option.Some(x))
.SingleOrDefaultAsync(cancellationToken)
.ConfigureAwait(false);
/// <summary>
/// Returns the only element of a sequence that satisfies a specified condition as an <see cref="Option{T}" /> or a <see cref="Option{T}.None" /> value if no such element exists; this method throws an exception if more than one element satisfies the condition.
/// </summary>
/// <typeparam name="TSource">the inner type of the queryable.</typeparam>
public static async Task<Option<TSource>> SingleOrNoneAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default)
where TSource : notnull
=> await source
.Where(predicate)
.Select(x => Option.Some(x))
.SingleOrDefaultAsync(cancellationToken)
.ConfigureAwait(false);
}