Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.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
C# 实体框架返回延迟加载查询,并在另一个方法中应用过滤器_C#_Entity Framework_Linq To Entities_Lazy Loading - Fatal编程技术网

C# 实体框架返回延迟加载查询,并在另一个方法中应用过滤器

C# 实体框架返回延迟加载查询,并在另一个方法中应用过滤器,c#,entity-framework,linq-to-entities,lazy-loading,C#,Entity Framework,Linq To Entities,Lazy Loading,我有一个大基查询,我想在各种方法中添加条件Where过滤器。我希望使用延迟加载在方法中创建基本查询,并在我添加条件where子句的各个位置调用它,然后在最后执行 我试过这样的方法: public IEnumerable<MyModel> GetBaseQuery() { IEnumerable<MyModel> base= context.MyTable1.Join(...{lot of joins here}) .Select{

我有一个大基查询,我想在各种方法中添加条件Where过滤器。我希望使用延迟加载在方法中创建基本查询,并在我添加条件where子句的各个位置调用它,然后在最后执行

我试过这样的方法:

public IEnumerable<MyModel> GetBaseQuery()
{

    IEnumerable<MyModel> base= context.MyTable1.Join(...{lot of joins here})
        .Select{
        ...select all of required fields for my model
        }
    return base;
}

public IEnumerable<MyModel> GetResultsByKeyword()
{
    IEnumerable<MyModel> model= GetBaseQuery();
    if(condition1 is true)
    {
        model = model.Where(x => x.Field1 == "Field1Value");
    }
    if(condition2 is true)
    {
        model = model.Where(x => x.Field2 == "Field2Value");
    }

    return model.Tolist(); //execute the query when calling ToList()
}
public IQueryable<MyModel> GetBaseQuery()
{

    var base = context.MyTable1. . . .
    return base;
}

public ICollection<MyModel> GetResultsByKeyword()
{
    IQueryable<MyModel> qry = GetBaseQuery();
    if (condition1 is true)
    {
        qry = qry.Where(x => x.Field1 == "Field1Value");
    }
    if (condition2 is true)
    {
        qry = qry.Where(x => x.Field2 == "Field2Value");
    }

    return qry.Tolist(); //execute the query when calling ToList()
}
public IEnumerable GetBaseQuery()
{
IEnumerable base=context.MyTable1.Join(…{lot of Join here})
.选择{
…选择我的模型的所有必填字段
}
返回基地;
}
public IEnumerable GetResultsByKeyword()
{
IEnumerable模型=GetBaseQuery();
如果(条件1为真)
{
模型=模型。其中(x=>x.Field1==“Field1Value”);
}
如果(条件2为真)
{
模型=模型。其中(x=>x.Field2==“Field2Value”);
}
return model.Tolist();//调用Tolist()时执行查询
}

上面没有返回任何结果,但是如果我将所有内容都放在一个大方法中,那么它就会执行我希望它执行的操作。我的实际设计可能有7种左右不同的方法可以调用“基本”查询,如果查询需要编辑以添加更多字段,我会尽量避免在每个方法中复制基本查询,以便1)可读性和2)持续维护

这两条注释指出了代码的两个问题。使用IEnumerable将在计算其他查询谓词之前运行查询,并在内存中计算它们。其次,
。如果
不改变IQueryable/IEnumerable,它将返回一个新的IQueryable/IEnumerable

所以应该是这样的:

public IEnumerable<MyModel> GetBaseQuery()
{

    IEnumerable<MyModel> base= context.MyTable1.Join(...{lot of joins here})
        .Select{
        ...select all of required fields for my model
        }
    return base;
}

public IEnumerable<MyModel> GetResultsByKeyword()
{
    IEnumerable<MyModel> model= GetBaseQuery();
    if(condition1 is true)
    {
        model = model.Where(x => x.Field1 == "Field1Value");
    }
    if(condition2 is true)
    {
        model = model.Where(x => x.Field2 == "Field2Value");
    }

    return model.Tolist(); //execute the query when calling ToList()
}
public IQueryable<MyModel> GetBaseQuery()
{

    var base = context.MyTable1. . . .
    return base;
}

public ICollection<MyModel> GetResultsByKeyword()
{
    IQueryable<MyModel> qry = GetBaseQuery();
    if (condition1 is true)
    {
        qry = qry.Where(x => x.Field1 == "Field1Value");
    }
    if (condition2 is true)
    {
        qry = qry.Where(x => x.Field2 == "Field2Value");
    }

    return qry.Tolist(); //execute the query when calling ToList()
}
public IQueryable GetBaseQuery()
{
var base=context.MyTable1。
返回基地;
}
public ICollection GetResultsByKeyword()
{
IQueryable qry=GetBaseQuery();
如果(条件1为真)
{
qry=qry,其中(x=>x.Field1==“Field1Value”);
}
如果(条件2为真)
{
qry=qry,其中(x=>x.Field2==“Field2Value”);
}
返回qry.Tolist();//调用Tolist()时执行查询
}

model=model.Where(…)与您的特定问题无关,但通常最好以IQueryable而不是IEnumerable返回。您是否已在DbContext中将属性声明为virtual,例如“public virtual DbSet MyProperty{get;set;}”?谢谢!将类型更改为IQueryable解决了此问题。实际上,我刚刚输入了错误的操作代码,并且已经通过model=model分配了模型。其中。。。在我的实际代码中。我已经更新了OP。我感谢您的帮助和解释。