参数' p'未绑定在指定的LINQ to Entities查询表达式中

时间:2017-02-07 12:07:39

标签: c# entity-framework linq

我正在尝试使用自定义帮助文件进行快速测试以创建自己的linq查询。但是当我运行查询时,我得到一个错误说

.Call System.Linq.Queryable.OrderBy(
    .Call System.Linq.Queryable.OrderBy(
        .Call System.Linq.Queryable.OrderBy(
            .Call System.Linq.Queryable.OrderBy(
                .Call System.Linq.Queryable.OrderBy(
                    .Call System.Linq.Queryable.OrderBy(
                        .Call System.Linq.Queryable.OrderBy(
                            .Call System.Linq.Queryable.Join(
                                .Call System.Linq.Queryable.Join(
                                    .Call .Constant<System.Data.Entity.Core.Objects.ObjectQuery`1[S20_Workflow.Domain.Model.Persoon]>(System.Data.Entity.Core.Objects.ObjectQuery`1[S20_Workflow.Domain.Model.Persoon]).MergeAs(.Constant<System.Data.Entity.Core.Objects.MergeOption>(AppendOnly))
                                    ,
                                    .Call .Constant<System.Data.Entity.Core.Objects.ObjectQuery`1[S20_Workflow.Domain.Model.Verhuringen]>(System.Data.Entity.Core.Objects.ObjectQuery`1[S20_Workflow.Domain.Model.Verhuringen]).MergeAs(.Constant<System.Data.Entity.Core.Objects.MergeOption>(AppendOnly))
                                    ,
                                    '(.Lambda #Lambda1<System.Func`2[S20_Workflow.Domain.Model.Persoon,System.Int32]>),
                                    '(.Lambda #Lambda2<System.Func`2[S20_Workflow.Domain.Model.Verhuringen,System.Int32]>),
                                    '(.Lambda #Lambda3<System.Func`3[S20_Workflow.Domain.Model.Persoon,S20_Workflow.Domain.Model.Verhuringen,<>f__AnonymousType0`2[S20_Workflow.Domain.Model.Persoon,S20_Workflow.Domain.Model.Verhuringen]]>))
                                ,
                                .Call .Constant<System.Data.Entity.Core.Objects.ObjectQuery`1[S20_Workflow.Domain.Model.Eenheid]>(System.Data.Entity.Core.Objects.ObjectQuery`1[S20_Workflow.Domain.Model.Eenheid]).MergeAs(.Constant<System.Data.Entity.Core.Objects.MergeOption>(AppendOnly))
                                ,
                                '(.Lambda #Lambda4<System.Func`2[<>f__AnonymousType0`2[S20_Workflow.Domain.Model.Persoon,S20_Workflow.Domain.Model.Verhuringen],System.Int32]>),
                                '(.Lambda #Lambda5<System.Func`2[S20_Workflow.Domain.Model.Eenheid,System.Int32]>),
                                '(.Lambda #Lambda6<System.Func`3[<>f__AnonymousType0`2[S20_Workflow.Domain.Model.Persoon,S20_Workflow.Domain.Model.Verhuringen],S20_Workflow.Domain.Model.Eenheid,S2020.Personen.ViewModels.PersoonDTO]>))
                            ,
                            '(.Lambda #Lambda7<System.Func`2[S2020.Personen.ViewModels.PersoonDTO,System.String]>)),
                        '(.Lambda #Lambda8<System.Func`2[S2020.Personen.ViewModels.PersoonDTO,System.String]>)),
                    '(.Lambda #Lambda9<System.Func`2[S2020.Personen.ViewModels.PersoonDTO,System.String]>)),
                '(.Lambda #Lambda10<System.Func`2[S2020.Personen.ViewModels.PersoonDTO,System.String]>)),
            '(.Lambda #Lambda11<System.Func`2[S2020.Personen.ViewModels.PersoonDTO,System.String]>)),
        '(.Lambda #Lambda12<System.Func`2[S2020.Personen.ViewModels.PersoonDTO,System.String]>)),
    '(.Lambda #Lambda13<System.Func`2[S2020.Personen.ViewModels.PersoonDTO,System.String]>))

.Lambda #Lambda1<System.Func`2[S20_Workflow.Domain.Model.Persoon,System.Int32]>(S20_Workflow.Domain.Model.Persoon $persoon)
{
    $persoon.ComputerNr
}

.Lambda #Lambda2<System.Func`2[S20_Workflow.Domain.Model.Verhuringen,System.Int32]>(S20_Workflow.Domain.Model.Verhuringen $verhuring)
{
    $verhuring.PersoonsID
}

.Lambda #Lambda3<System.Func`3[S20_Workflow.Domain.Model.Persoon,S20_Workflow.Domain.Model.Verhuringen,<>f__AnonymousType0`2[S20_Workflow.Domain.Model.Persoon,S20_Workflow.Domain.Model.Verhuringen]]>(
    S20_Workflow.Domain.Model.Persoon $persoon,
    S20_Workflow.Domain.Model.Verhuringen $verhuring) {
    .New <>f__AnonymousType0`2[S20_Workflow.Domain.Model.Persoon,S20_Workflow.Domain.Model.Verhuringen](
        $persoon,
        $verhuring)
}

.Lambda #Lambda4<System.Func`2[<>f__AnonymousType0`2[S20_Workflow.Domain.Model.Persoon,S20_Workflow.Domain.Model.Verhuringen],System.Int32]>(<>f__AnonymousType0`2[S20_Workflow.Domain.Model.Persoon,S20_Workflow.Domain.Model.Verhuringen] $t)
{
    ($t.verhuring).Eenheid
}

.Lambda #Lambda5<System.Func`2[S20_Workflow.Domain.Model.Eenheid,System.Int32]>(S20_Workflow.Domain.Model.Eenheid $eenheid)
{
    $eenheid.ComputerNr
}

.Lambda #Lambda6<System.Func`3[<>f__AnonymousType0`2[S20_Workflow.Domain.Model.Persoon,S20_Workflow.Domain.Model.Verhuringen],S20_Workflow.Domain.Model.Eenheid,S2020.Personen.ViewModels.PersoonDTO]>(
    <>f__AnonymousType0`2[S20_Workflow.Domain.Model.Persoon,S20_Workflow.Domain.Model.Verhuringen] $t,
    S20_Workflow.Domain.Model.Eenheid $eenheid) {
    .New S2020.Personen.ViewModels.PersoonDTO(){
        ComputerNr = ($t.persoon).ComputerNr,
        FAMILIENAAM = ($t.persoon).FAMILIENAAM,
        VOORNAAM = ($t.persoon).VOORNAAM,
        NAAM = ($t.persoon).NAAM,
        ADRES = ($t.persoon).ADRES,
        Een = $eenheid
    }
}

.Lambda #Lambda7<System.Func`2[S2020.Personen.ViewModels.PersoonDTO,System.String]>(S2020.Personen.ViewModels.PersoonDTO $p)
{
    $p.FAMILIENAAM
}

.Lambda #Lambda8<System.Func`2[S2020.Personen.ViewModels.PersoonDTO,System.String]>(S2020.Personen.ViewModels.PersoonDTO $p)
{
    ($p.Een).GEMEENTECODE
}

.Lambda #Lambda9<System.Func`2[S2020.Personen.ViewModels.PersoonDTO,System.String]>(S2020.Personen.ViewModels.PersoonDTO $p)
{
    ($p.Een).WIJKCODE
}

.Lambda #Lambda10<System.Func`2[S2020.Personen.ViewModels.PersoonDTO,System.String]>(S2020.Personen.ViewModels.PersoonDTO $p)
{
    ($p.Een).GROEPCODE
}

.Lambda #Lambda11<System.Func`2[S2020.Personen.ViewModels.PersoonDTO,System.String]>(S2020.Personen.ViewModels.PersoonDTO $p)
{
    ($p.Een).STRAATCODE
}

.Lambda #Lambda12<System.Func`2[S2020.Personen.ViewModels.PersoonDTO,System.String]>(S2020.Personen.ViewModels.PersoonDTO $p)
{
    ($p.Een).GEBOUWCODE
}

.Lambda #Lambda13<System.Func`2[S2020.Personen.ViewModels.PersoonDTO,System.String]>(S2020.Personen.ViewModels.PersoonDTO $p)
{
    ($p.Een).HUISNR
}

我真的不知道为什么会发生这种情况,因为显然$ p作为参数传递,不是吗?

代码&#34; p&#34;添加如下:

private static IOrderedQueryable<T> OrderingHelper<T>(IQueryable<T> source, string propertyName, bool descending, bool anotherLevel)
{
    var parts = propertyName.Split('.');
    var param = Expression.Parameter(typeof(T), "p");

    Expression parent = parts.Aggregate<string, Expression>(param, Expression.Property);
    Expression conversion = Expression.Convert(parent, typeof(object));
    var uexpr = (UnaryExpression)conversion;
    var pexpr = (MemberExpression)uexpr.Operand;
    var customAttributes = pexpr.Member.GetCustomAttributes(typeof(SortComboHelper), true);
    var sort = customAttributes[0] as SortComboHelper;
    var child = ((MemberExpression)parent).Expression;

    var call = sort.ConvertToLambda<T>(source, child as MemberExpression, descending, anotherLevel);

    return (IOrderedQueryable<T>)source.Provider.CreateQuery<T>(call);
}

我也在下面的部分中使用$ p,但我不认为错误是因为这个p,因为如果我将此更改为另一个字母,则错误仍然相同

public Expression ConvertToLambda<T>(IQueryable<T> source, MemberExpression parent, bool descending, bool anotherLevel)
    {
        var call = source.Expression;
        foreach (var s in _param)
        {
            var property = Expression.Property(parent, s);
            var param = Expression.Parameter(typeof(T), "p");
            var mySortExpression = Expression.Lambda(property, param);

            call = Expression.Call(
                typeof(Queryable),
                (!anotherLevel ? "OrderBy" : "ThenBy") + (descending ? "Descending" : string.Empty),
                new[] { typeof(T), property.Type },
                call,
                Expression.Quote(mySortExpression));
        }
        return call;
    }

1 个答案:

答案 0 :(得分:2)

您传递给MemberExpression parent方法的ConvertToLambda绑定到不同的参数

var param = Expression.Parameter(typeof(T), "p");

来自OrderingHelper方法。但是当你在里面创建一个lambda表达式时,你使用一个不同的参数(虽然参数的名称和类型是相同的,它们仍然是不同的实例),因此原始参数没有绑定到该lambda,你得到了异常问题。

您应该从外部将原始参数传递给ConvertToLambda并在里面使用它:

public Expression ConvertToLambda<T>(IQueryable<T> source, ParameterExpression param, MemberExpression parent, bool descending, bool anotherLevel)
{
    var call = source.Expression;
    foreach (var s in _param)
    {
        var property = Expression.Property(parent, s);
        var mySortExpression = Expression.Lambda(property, param);

        call = Expression.Call(
            typeof(Queryable),
            (!anotherLevel ? "OrderBy" : "ThenBy") + (descending ? "Descending" : string.Empty),
            new[] { typeof(T), property.Type },
            call,
            Expression.Quote(mySortExpression));
    }
    return call;
}

private static IOrderedQueryable<T> OrderingHelper<T>(IQueryable<T> source, string propertyName, bool descending, bool anotherLevel)
{
    // ...
    var call = sort.ConvertToLambda<T>(source, param, child as MemberExpression, descending, anotherLevel);
    // ...
}