Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
Linq &引用;注入;iQueryTable中的WHERE子句,在它执行GROUP BY之前_Linq - Fatal编程技术网

Linq &引用;注入;iQueryTable中的WHERE子句,在它执行GROUP BY之前

Linq &引用;注入;iQueryTable中的WHERE子句,在它执行GROUP BY之前,linq,Linq,我有两个表:Foo和Bar,它们参与1:M关系,也就是说,每个Foo都有许多相关的Bar实例 我有这样一个问题: IQueryable< IGrouping< Foo, Bar > > GetGroups() { return this.dbContext .Bar .Include( bar => bar.Foo ) .GroupBy( bar => bar.Foo ); } 但是,如果GetGro

我有两个表:
Foo
Bar
,它们参与1:M关系,也就是说,每个
Foo
都有许多相关的
Bar
实例

我有这样一个问题:

IQueryable< IGrouping< Foo, Bar > > GetGroups() {

    return this.dbContext
        .Bar
        .Include( bar => bar.Foo )
        .GroupBy( bar => bar.Foo );
}
但是,如果
GetGroups
的使用者希望对返回的
行应用谓词,该怎么办

简单的解决方案是将其作为
GetGroups
的参数提供:

    IQueryable< IGrouping< Foo, Bar > > GetGroups( Func<Bar,Boolean> barPredicate ) {

    return this.dbContext
        .Bar
        .Include( bar => bar.Foo )
        .Where( barPredicate )
        .GroupBy( bar => bar.Foo );
}

Include
GroupBy
语句之间,有没有办法检查
IQueryable
和“inject”my
Where
子句?

你能告诉我为什么在你的情况下不能选择直接的解决方案吗?我不知道您的情况,但可能最好是设法将其作为一个选项,而不是尝试插入
where
子句。可能有某种注入方法,但我想不出任何方法。@AdilMammadov
GetGroups
在我无法访问的库中。我只有一个
IQueryable
。您能告诉我为什么在您的情况下不能选择直接的解决方案吗?我不知道您的情况,但可能最好是设法将其作为一个选项,而不是尝试插入
where
子句。可能有某种注入方法,但我想不出任何方法。@AdilMammadov
GetGroups
在我无法访问的库中。我只有一个
IQueryable
    IQueryable< IGrouping< Foo, Bar > > GetGroups( Func<Bar,Boolean> barPredicate ) {

    return this.dbContext
        .Bar
        .Include( bar => bar.Foo )
        .Where( barPredicate )
        .GroupBy( bar => bar.Foo );
}
IQueryable<Bar> barsQuery = query.SelectMany( grp => grp );
barsQuery = barsQuery.Where( barPredicate );
query = barsQuery.GroupBy( bar => bar.Foo );