Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 在Predicatebuilder中使用Linq Any子句发出_Asp.net Mvc 3_Entity Framework_Linq To Entities_Linqkit - Fatal编程技术网

Asp.net mvc 3 在Predicatebuilder中使用Linq Any子句发出

Asp.net mvc 3 在Predicatebuilder中使用Linq Any子句发出,asp.net-mvc-3,entity-framework,linq-to-entities,linqkit,Asp.net Mvc 3,Entity Framework,Linq To Entities,Linqkit,我对LinqKit谓词生成器有问题。我过去曾在简单查询中使用过它,它工作得很好,但现在我尝试将它与语句中的Any子句一起使用,它似乎会给我随机结果。下面是我用来构建语句的代码。有人能看出我做错了什么吗?有没有更好更简单的方法来做我想做的事。我现在正在使用predicatebuilder,因为我正在构建一个非常复杂的查询,它可能包含嵌套的谓词等等,我没有看到其他简单的方法来实现这一点。我使用实体框架 if (cqv.ComplexQuery.IncludeOnAll) { Includep

我对LinqKit谓词生成器有问题。我过去曾在简单查询中使用过它,它工作得很好,但现在我尝试将它与语句中的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个以上的产品,而不是寻找同时拥有这两种产品或其中任何一种产品的任何客户,那么我gt的结果始终只是我加入条款的最后一个产品的客户。我会把我的更新代码放在下面

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子句吗?

我最终发现我需要在for循环中创建临时变量来保存值。当你这样做的时候,它不知何故知道如何立即解析值,谓词就可以工作了。

我的第一个修复程序似乎没有真正做到这一点,只是碰巧起了作用。问题是Predicatebuilder在使用变量之前似乎无法解析变量的值,所以当我与a.ProductType.ID进行比较时,它实际上是在检查它的上一个值,这就是为什么我认为我得到的是随机结果。当我将其放入值变量时,它在我只选择1个产品时起作用,因为它的最后一个值是我要查找的ID。当我开始选择多个产品时,它会在最后解析,因此该值是最后选择的模块的值。