1

LinqKitpredicatebuilderに問題があります。過去に単純なクエリに使用しましたが、正常に機能しましたが、現在、ステートメントのAny句で使用しようとしていますが、ランダムな結果が得られているようです。以下は、ステートメントの作成に使用しているコードです。誰かが私が間違っていることを見ることができますか?私がやりたいことをするためのより良い、より簡単な方法はありますか?ネストされた述語などを含む可能性のある非常に複雑なクエリが作成されているため、現在predicatebuilderを使用していますが、これを行う簡単な方法は他にありません。私はこれをエンティティフレームワークで使用しています。

if (cqv.ComplexQuery.IncludeOnAll)
{
    Includepredicate = PredicateBuilder.True<Customer>();
}
else
{
    Includepredicate = PredicateBuilder.False<Customer>();
}

inner = PredicateBuilder.True<Customer>();
if (a.Include == true || a.Exclude == true) 
{
    productinner = PredicateBuilder.True<CustomerProduct>();
    if (a.VersiondID != 0)
    {
        productinner = productinner.And(o => o.ProductTypeID == a.ProductType.ID  && o.VersionID == a.VersiondID);
        inner = inner.And(o => o.Products.Any(productinner.Compile()));
    }
    else
    {
        productinner = productinner.And(o => o.ProductTypeID == a.ProductType.ID);
        inner = inner.And(o => o.Products.Any(productinner.Compile()));
    }

    if (cqv.ComplexQuery.IncludeOnAll)
    {
        Includepredicate = Includepredicate.And(inner.Expand());
    }
    else
    {
        Includepredicate = Includepredicate.Or(inner.Expand());
    }
}

IncludedCustomers = UoW.Customers.AsExpandable().Where(Includepredicate).ToList();

//This second list does the exact query the first one should be doing so I could compare results.  The reuslts are totally different.  Not only are there more results using predicatebuilder but they seem random

List<Customer> test = UoW.Customers.Where(o => o.Products.Any(s => s.ProductTypeID == 1)).ToList();

述語ビルダーで問題をデバッグしようとする簡単な方法もわかりません。このクエリから作成されるSQLをすばやく判別する方法を知っている人はいますか?

編集 - - - - - - - - - - - - - - - - - - - - - - -

だから私は自分の問題の一部を解決しましたが、別の問題に遭遇しました。Any句とランダムな結果の問題は、a.ProductType.IDを使用して整数変数を設定し、その値を句で使用することで修正されました。それをしたら、期待した結果が得られました。今私の問題は、これらの製品の両方またはこれらの製品のいずれかを持っている顧客を探す代わりに、1つ以上を選択した場合、1つの製品が選択されたときにこれは正常に機能しますが、私が得た結果は常に私が条項を入れた最後の製品。更新したコードを下に置きます

foreach (CustomerProductQueryProduct a in cqv.ComplexQuery.Products)
{
    inner = PredicateBuilder.True<Customer>();
    if (a.Include == true || a.Exclude == true) 
    {
        value = a.ProductType.ID;
        productinner = PredicateBuilder.True<CustomerProduct>();
        if (a.VersiondID != 0)
        {
            productinner = productinner.And(s => s.ProductTypeID == value && s.VersionID == a.VersiondID);  
            inner = inner.And(o => o.Products.Any(productinner.Compile()));
        }
        else
        {
            productinner = productinner.And(s => s.ProductTypeID == value);
            inner = inner.And(o => o.Products.Any(productinner.Compile()));
        }

        if (cqv.ComplexQuery.IncludeOnAll)
        {
            Includepredicate = Includepredicate.And(inner.Expand());
        }
        else
        {
            Includepredicate = Includepredicate.Or(inner.Expand());
        }
    }
}

IncludedCustomers = UoW.Customers.AsExpandable().Where(Includepredicate).ToList();

PredicateBuilderは複数のAny句を処理できませんか?

4

1 に答える 1

0

値を保持するには、 for ループ内で一時変数を作成する必要があることが最終的にわかりました。それを行うと、値をすぐに解決することがどういうわけかわかり、述語が機能します。

于 2013-01-25T15:10:57.220 に答える