Question

I am having an issue with the LinqKit predicatebuilder. I have used it in the past for simple queries and it has worked fine but I am now trying to use it with an Any clause in the statement and it seems to be giving me random results. Below is the code I am using to build the statement. Can anyone see what I am doing wrong? Is there a better and easier way to do what I want to do. I am using predicatebuilder right now because there is a very complex query I am building which could contain nested predicates and such and I have seen no other easy way to do this. I am using this with entity framework.

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();

I don't see any easy way to try to debug issue with predicate builder either. Does anyone know of a quick way to determine the SQL that gets created from this query?

EDIT ----------------------------------------------

So I have solved part of my issue but run into another one. the issue with the Any clause and random results was fixed by me setting in integer variable with the a.ProductType.ID and using that value in the clause. Once I did that I got the results I expected. Now my issue is that even though this works fine when 1 product is select if I select naymore than 1 instead of either looking for any customers that have both of these products or either of these products the results I gt is always just the customer with the last product I put a clause in for. I will put my updated code below

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();

Is PredicateBuilder not able to handle multiple Any clauses?

Était-ce utile?

La solution

I finally figured out that I need to make temporary variable inside of my for loop to hold the value. When you do that it somehow knows to resolve the value immediately and the predicates work.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top