geekbot/Geekbot.net/Lib/Extensions/DbSetExtensions.cs

28 lines
1.2 KiB
C#
Raw Normal View History

2018-05-13 14:14:50 +02:00
using System;
2020-06-01 02:16:29 +02:00
using System.Collections.Generic;
2018-05-13 14:14:50 +02:00
using System.Linq;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
namespace Geekbot.net.Lib.Extensions
{
public static class DbSetExtensions
{
public static EntityEntry<T> AddIfNotExists<T>(this DbSet<T> dbSet, T entity, Expression<Func<T, bool>> predicate = null) where T : class, new()
{
var exists = predicate != null ? dbSet.Any(predicate) : dbSet.Any();
return !exists ? dbSet.Add(entity) : null;
}
2020-06-01 02:16:29 +02:00
// https://github.com/dotnet/efcore/issues/18124
public static IAsyncEnumerable<TEntity> AsAsyncEnumerable<TEntity>(this Microsoft.EntityFrameworkCore.DbSet<TEntity> obj) where TEntity : class
{
return Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.AsAsyncEnumerable(obj);
}
public static IQueryable<TEntity> Where<TEntity>(this Microsoft.EntityFrameworkCore.DbSet<TEntity> obj, System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate) where TEntity : class
{
return System.Linq.Queryable.Where(obj, predicate);
}
2018-05-13 14:14:50 +02:00
}
}