Files
Yi.Framework/CC.Yi.DAL/BaseDal.cs
454313500@qq.com 2e5b991db0 v1.0.0
2021-03-20 15:02:23 +08:00

93 lines
2.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using CC.Yi.IDAL;
using CC.Yi.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Linq.Expressions;
namespace CC.Yi.DAL
{
public class BaseDal<T> : IBaseDal<T> where T : class, new()
{
public DataContext Db;
public BaseDal(DataContext _Db)
{
Db = _Db;
}
public IQueryable<T> GetEntities(Expression<Func<T, bool>> whereLambda)
{
return Db.Set<T>().Where(whereLambda).AsQueryable();
}
public IQueryable<T> GetAllEntities()
{
return Db.Set<T>().AsQueryable();
}
public int GetCount(Expression<Func<T, bool>> whereLambda)
{
return Db.Set<T>().Where(whereLambda).Count();
}
public IQueryable<IGrouping<S, T>> GetGroup<S>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> groupByLambda) //分组
{
return Db.Set<T>().Where(whereLambda).GroupBy<T, S>(groupByLambda).AsQueryable();
}
public IQueryable<T> GetPageEntities<S>(int pageSize, int pageIndex, out int total, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderByLambda, bool isAsc)
{
total = Db.Set<T>().Where(whereLambda).Count();
if (isAsc)
{
var pageData = Db.Set<T>().Where(whereLambda)
.OrderBy<T, S>(orderByLambda)
.Skip(pageSize * (pageIndex - 1))
.Take(pageSize).AsQueryable();
return pageData;
}
else
{
var pageData = Db.Set<T>().Where(whereLambda)
.OrderByDescending<T, S>(orderByLambda)
.Skip(pageSize * (pageIndex - 1))
.Take(pageSize).AsQueryable();
return pageData;
}
}
public T Add(T entity)
{
Db.Set<T>().Add(entity);
//Db.SaveChanges();
return entity;
}
public bool Update(T entity)
{
//所有字段均修改
Db.Entry<T>(entity).State = EntityState.Modified;
//return Db.SaveChanges() > 0;
return true;
}
public bool Delete(T entity)
{
Db.Entry<T>(entity).State = EntityState.Deleted;
//return Db.SaveChanges() > 0;
return true;
}
public bool Detete(int id)
{
var entity = Db.Set<T>().Find(id);//根据id找到实体
Db.Set<T>().Remove(entity);//由于这里先Find找到了实体所以这里可以用Remove标记该实体要移除删除。如果不是先Find到实体就需要用System.Data.Entity.EntityState.Deleted
return true;
}
}
}