(精华)2020年7月23日 C#基础知识点 表达式目录数实现组合继承(EF动态查询)

//定义数据模型类
public class Base_UserTestDTO : Base_User
{
    public string DepartmentName { get; set; }
}

//即BaseBusiness中的Service
var db = DbFactory.GetRepository();
Expression<Func<Base_User, Base_Department, Base_UserTestDTO>> select = (a, b) => new Base_UserTestDTO
{
    DepartmentName = b.Name
};
select = select.BuildExtendSelectExpre();
var q = from a in db.GetIQueryable<Base_User>().AsExpandable()
        join b in db.GetIQueryable<Base_Department>() on a.DepartmentId equals b.Id into ab
        from b in ab.DefaultIfEmpty()
        select @select.Invoke(a, b);
//筛选
var where = LinqHelper.True<Base_UserTestDTO>();
where = where.And(x => x.UserName == "Admin");

//获取筛选数据
var list = q.Where(where).ToList();
public static Expression<Func<TBase, T1, TResult>> BuildExtendSelectExpre<TBase, T1, TResult>(this Expression<Func<TBase, T1, TResult>> expression)
{
    return GetExtendSelectExpre<TBase, TResult, Func<TBase, T1, TResult>>(expression);
}
private static Expression<TDelegate> GetExtendSelectExpre<TBase, TResult, TDelegate>(Expression<TDelegate> expression)
{
    NewExpression newBody = Expression.New(typeof(TResult));
    MemberInitExpression oldExpression = (MemberInitExpression)expression.Body;

    ParameterExpression[] oldParamters = expression.Parameters.ToArray();
    List<string> existsProperties = new List<string>();
    oldExpression.Bindings.ToList().ForEach(aBinding =>
    {
        existsProperties.Add(aBinding.Member.Name);
    });

    List<MemberBinding> newBindings = new List<MemberBinding>();
    typeof(TBase).GetProperties().Where(x => !existsProperties.Contains(x.Name)).ToList().ForEach(aProperty =>
    {
        if (typeof(TResult).GetMembers().Any(x => x.Name == aProperty.Name))
        {
            MemberBinding newMemberBinding = null;
            var valueExpre = Expression.Property(oldParamters[0], aProperty.Name);
            if (typeof(TBase).IsAssignableFrom(typeof(TResult)))
            {
                newMemberBinding = Expression.Bind(aProperty, valueExpre);
            }
            else
            {
                newMemberBinding = Expression.Bind(typeof(TResult).GetProperty(aProperty.Name), valueExpre);
            }
            newBindings.Add(newMemberBinding);
        }
    });

    newBindings.AddRange(oldExpression.Bindings);

    var body = Expression.MemberInit(newBody, newBindings.ToArray());
    var resExpression = Expression.Lambda<TDelegate>(body, oldParamters);

    return resExpression;
}
/// <summary>
    /// Linq操作帮助类
    /// </summary>
    public static class LinqHelper
    {
        /// <summary>
        /// 创建初始条件为True的表达式
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static Expression<Func<T,bool>> True<T>()
        {
            return x => true;
        }

        /// <summary>
        /// 创建初始条件为False的表达式
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static Expression<Func<T, bool>> False<T>()
        {
            return x => false;
        }
    }

猜你喜欢

转载自blog.csdn.net/aa2528877987/article/details/107539166