如何将其他lambda表达式添加到IQueryable CreateQuery

时间:2017-12-28 21:01:00

标签: c# lambda expression

这是我的原始选择查询,适用于一个字段" SurveyName":

    public static IQueryable Select(this IQueryable source, string selector, params object[] values)
    {
        LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, null, "SurveyName", values); //SurveyName is hard coded here as example
        return source.Provider.CreateQuery(
            Expression.Call(
                typeof(Queryable), "Select",
                new Type[] { source.ElementType, lambda.Body.Type },
                source.Expression, Expression.Quote(lambda)));
    }

我想添加第二个lambda表达式,我尝试了下面的内容:

    public static IQueryable Select(this IQueryable source, string selector, params object[] values)
    {
        LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, null, "SurveyName", values); //SurveyName is hard coded as example
        LambdaExpression lambda2 = DynamicExpression.ParseLambda(source.ElementType, null, "SurveyHeaderName", values); //SurveyHeaderName is hard coded as example

        return source.Provider.CreateQuery(
            Expression.Call(
                typeof(Queryable), "Select",
                new Type[] { source.ElementType, lambda.Body.Type, lambda2.Body.Type },
                source.Expression, Expression.Quote(lambda)));
    }

哪个给我

  

没有通用方法'选择' on type' System.Linq.Queryable'是   与提供的类型参数和参数兼容。没有类型   如果方法是非泛型的

,则应提供参数

如何实现添加额外的lambda表达式(我的意思是附加字段Select SurveyName, SurveyHeaderName

听到" lambda"调试中的屏幕截图

enter image description here

为了提供更多细节,为什么我使用上面的代码,这里是前端部分:

public class SurveyQuestionSurveyHeaderSurvey
{
    public SurveyQuestion SurveyQuestion { get; set; }
    public SurveyHeader SurveyHeader { get; set; }
    public Survey Survey { get; set; }

    public String SurveyName { get; set; }
    public String SurveyHeaderName { get; set; }
}

public IEnumerable<SurveyQuestionReportViewModel> SurveyQuestionReportExcel(IDataTablesRequest dataTablesRequest, out int totalCount)
{
    var res0 = from sq in _uow.Repository<SurveyQuestion>().Queryable()
               join s in _uow.Repository<Survey>().Queryable()
               on sq.SurveyHeader.SurveyId equals s.SurveyId
               select new SurveyQuestionSurveyHeaderSurvey { SurveyQuestion = sq, Survey = s, SurveyHeader = sq.SurveyHeader, SurveyName = s.Name };


    List<Expression<Func<object, bool>>> _expression;
    List<Expression<Func<object, object>>> _includes;

    _includes = new List<Expression<Func<object, object>>>();
    _expression = new List<Expression<Func<object, bool>>>();
    Func<IQueryable<object>, IOrderedQueryable<object>> _orderBy;

    if (dataTablesRequest.GetSearchListParameter() != null)
        _expression.Add(DynamicQueryable.WhereLambdaExpression<object>(dataTablesRequest.GetSearchListParameter()));

    if (dataTablesRequest.GetOrderByParameter() != null)
        _orderBy = p => p.OrderBy(dataTablesRequest.GetOrderByParameter());
    else
        _orderBy = p => p.OrderBy("ID Desc");

    var fields = String.Join(",", dataTablesRequest.Columns.Select(x => x.Data).ToArray()).TrimEnd(',');
    fields = "SurveyName";
    var toto = res0.Select(fields, null, _expression, _orderBy, _includes, dataTablesRequest.Start, dataTablesRequest.Length);


    var result = res0.Include(p => p.SurveyHeader.Survey).Where(x => x.SurveyHeader.SurveyId == 3074)
       .Select(x => new SurveyQuestionReportViewModel
       {
           SurveyQuestionId = x.SurveyQuestion.SurveyQuestionId,
           SurveyName = x.SurveyHeader.Survey.Name,
           Name = "Genel Sonuç",
           HeaderName = x.SurveyHeader.Name,
           QuestionName = x.SurveyQuestion.Description,
       });
    totalCount = 10;
    return result;

}

1 个答案:

答案 0 :(得分:0)

我首先通过以下方式构建我的lambda表达式:

    static Expression<Func<T, T>> BuildLambda<T>()
    {
        var createdType = typeof(T);
        var displayValueParam = Expression.Parameter(typeof(T), "Param_0");
        var ctor = Expression.New(createdType);
        var bindings = new List<MemberAssignment>();
        foreach (var propertyInfo in typeof(T).GetProperties())
        {
            Expression left = Expression.Property(displayValueParam, propertyInfo);
            var displayValueProperty = propertyInfo;
            bindings.Add(Expression.Bind(displayValueProperty, left));

        }
        var memberInit = Expression.MemberInit(ctor, bindings);
        return
            Expression.Lambda<Func<T, T>>(memberInit, displayValueParam);
    }

我的选择是:

    public static IQueryable<T> Select<T>(this IQueryable source,  IDataTablesRequest dataTablesRequest, out int totalCount)
    {

        var lambda = BuildLambda<T>();
        var query = source.Provider.CreateQuery(
            Expression.Call(
                typeof(Queryable), "Select",
                new Type[] { source.ElementType, lambda.Body.Type },
                source.Expression
                , Expression.Quote((Expression)lambda)
                ));
        if (dataTablesRequest.GetSearchListParameter() != null)
            query = query.Where(dataTablesRequest.GetSearchListParameter());
        totalCount = query.Count();


        if (dataTablesRequest.GetOrderByParameter() != null)
            query = query.OrderBy(dataTablesRequest.GetOrderByParameter(), null);

        query = query.Skip(dataTablesRequest.Start).Take(dataTablesRequest.Length);

        return (IQueryable<T>)query;
    }