Files
juipnet/Infrastructure/Hncore.Infrastructure/EF/DbSetExtension.cs
“wanyongkang” ed3b2c653e 接口文件
2024-04-10 13:55:27 +08:00

122 lines
4.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Hncore.Infrastructure.Data;
using Hncore.Infrastructure.DDD;
using Hncore.Infrastructure.EntitiesExtension;
namespace Hncore.Infrastructure.EF
{
public static class DbSetExtension
{
public static TEntity GetOne<TEntity>(this DbSet<TEntity> dbSet, Expression<Func<TEntity, bool>> exp)
where TEntity : class
{
return dbSet.AsNoTracking().FirstOrDefault(exp);
}
public static Task<TEntity> GetOneAsync<TEntity>(this DbSet<TEntity> dbSet,
Expression<Func<TEntity, bool>> exp) where TEntity : class
{
return dbSet.AsNoTracking().FirstOrDefaultAsync(exp);
}
public static PageData<TEntity> GetList<TEntity, TId>(this DbSet<TEntity> dbSet,
Expression<Func<TEntity, bool>> exp, int pagesize, int pageindex, bool istotal)
where TEntity : class, IEntity<TId>
{
return dbSet.AsNoTracking()
.Where(exp)
.OrderByDescending(t => t.Id)
.ListPager(pagesize, pageindex, istotal);
}
public static Task<PageData<TEntity>> GetListAsync<TEntity>(this DbSet<TEntity> dbSet,
Expression<Func<TEntity, bool>> exp, int pagesize, int pageindex, bool istotal)
where TEntity : class
{
return dbSet.AsNoTracking()
.Where(exp)
.ListPagerAsync(pagesize, pageindex, istotal);
}
public static List<TEntity> GetList<TEntity>(this DbSet<TEntity> dbSet, Expression<Func<TEntity, bool>> exp)
where TEntity : class, IEntity
{
return dbSet.AsNoTracking()
.Where(exp)
.ToList();
}
public static Task<List<TEntity>> GetListAsync<TEntity>(this DbSet<TEntity> dbSet,
Expression<Func<TEntity, bool>> exp)
where TEntity : class, IEntity
{
return dbSet.AsNoTracking()
.Where(exp)
.ToListAsync();
}
public static IQueryable<TEntity> GetListQueryable<TEntity>(this DbSet<TEntity> dbSet,
Expression<Func<TEntity, bool>> exp)
where TEntity : class, IEntity
{
return dbSet.AsNoTracking()
.Where(exp);
}
public static bool Exists<TEntity>(this DbSet<TEntity> dbSet, Expression<Func<TEntity, bool>> exp)
where TEntity : class, IEntity
{
return dbSet.AsNoTracking()
.Any(exp);
}
public static Task<bool> ExistsAsync<TEntity>(this DbSet<TEntity> dbSet,
Expression<Func<TEntity, bool>> exp)
where TEntity : class, IEntity
{
return dbSet.AsNoTracking()
.AnyAsync(exp);
}
public static List<TEntity> TopN<TEntity>(this DbSet<TEntity> dbSet,
Expression<Func<TEntity, bool>> condition, int topN)
where TEntity : class, IEntity
{
return dbSet.AsNoTracking()
.Where(condition)
.TopN(topN)
.ToList();
}
public static Task<List<TEntity>> TopNAsync<TEntity>(this DbSet<TEntity> dbSet,
Expression<Func<TEntity, bool>> condition, int topN)
where TEntity : class, IEntity
{
return dbSet.AsNoTracking()
.Where(condition)
.TopN(topN)
.ToListAsync();
}
public static IQueryable<TEntity> GetQueryable<TEntity>(this DbSet<TEntity> dbSet) where TEntity : class
{
return dbSet.AsNoTracking();
}
public static TEntity FindById<TEntity>(this DbSet<TEntity> dbSet,object id) where TEntity : class
{
return dbSet.Find(id);
}
public static Task<TEntity> FindByIdAsync<TEntity>(this DbSet<TEntity> dbSet, object id) where TEntity : class
{
return dbSet.FindAsync(id);
}
}
}