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

110 lines
4.0 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 QueryExtension
{
public static TEntity GetOne<TEntity, TId>(this DbContext dbcontext, Expression<Func<TEntity, bool>> exp)
where TEntity : class, IEntity<TId>
{
return dbcontext.Set<TEntity>().AsNoTracking()
.FirstOrDefault(exp);
}
public static Task<TEntity> GetOneAsync<TEntity, TId>(this DbContext dbcontext,
Expression<Func<TEntity, bool>> exp) where TEntity : class, IEntity<TId>
{
return dbcontext.Set<TEntity>().AsNoTracking()
.FirstOrDefaultAsync(exp);
}
public static PageData<TEntity> GetList<TEntity, TId>(this DbContext dbcontext,
Expression<Func<TEntity, bool>> exp, int pagesize, int pageindex, bool istotal)
where TEntity : class, IEntity<TId>
{
return dbcontext.Set<TEntity>().AsNoTracking()
.Where(exp)
.OrderByDescending(t => t.Id)
.ListPager(pagesize, pageindex, istotal);
}
public static Task<PageData<TEntity>> GetListAsync<TEntity, TId>(this DbContext dbcontext,
Expression<Func<TEntity, bool>> exp, int pagesize, int pageindex, bool istotal)
where TEntity : class, IEntity<TId>
{
return dbcontext.Set<TEntity>().AsNoTracking()
.Where(exp)
.OrderByDescending(t => t.Id)
.ListPagerAsync(pagesize, pageindex, istotal);
}
public static List<TEntity> GetList<TEntity, TId>(this DbContext dbcontext, Expression<Func<TEntity, bool>> exp)
where TEntity : class, IEntity<TId>
{
return dbcontext.Set<TEntity>().AsNoTracking()
.Where(exp)
.ToList();
}
public static Task<List<TEntity>> GetListAsync<TEntity, TId>(this DbContext dbcontext,
Expression<Func<TEntity, bool>> exp)
where TEntity : class, IEntity<TId>
{
return dbcontext.Set<TEntity>().AsNoTracking()
.Where(exp)
.ToListAsync();
}
public static IQueryable<TEntity> GetListQueryable<TEntity, TId>(this DbContext dbcontext,
Expression<Func<TEntity, bool>> exp)
where TEntity : class, IEntity<TId>
{
return dbcontext.Set<TEntity>().AsNoTracking()
.Where(exp);
}
public static bool Exists<TEntity, TId>(this DbContext dbcontext, Expression<Func<TEntity, bool>> exp)
where TEntity : class, IEntity<TId>
{
return dbcontext.Set<TEntity>().AsNoTracking()
.Any(exp);
}
public static Task<bool> ExistsAsync<TEntity, TId>(this DbContext dbcontext,
Expression<Func<TEntity, bool>> exp)
where TEntity : class, IEntity<TId>
{
return dbcontext.Set<TEntity>().AsNoTracking()
.AnyAsync(exp);
}
public static List<TEntity> TopN<TEntity, TId>(this DbContext dbcontext,
Expression<Func<TEntity, bool>> condition, int topN)
where TEntity : class, IEntity<TId>
{
return dbcontext.Set<TEntity>().AsNoTracking()
.Where(condition)
.TopN(topN)
.ToList();
}
public static Task<List<TEntity>> TopNAsync<TEntity, TId>(this DbContext dbcontext,
Expression<Func<TEntity, bool>> condition, int topN)
where TEntity : class, IEntity<TId>
{
return dbcontext.Set<TEntity>().AsNoTracking()
.Where(condition)
.TopN(topN)
.ToListAsync();
}
}
}