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”myWhere
子句?你能告诉我为什么在你的情况下不能选择直接的解决方案吗?我不知道您的情况,但可能最好是设法将其作为一个选项,而不是尝试插入where
子句。可能有某种注入方法,但我想不出任何方法。@AdilMammadovGetGroups
在我无法访问的库中。我只有一个IQueryable
。您能告诉我为什么在您的情况下不能选择直接的解决方案吗?我不知道您的情况,但可能最好是设法将其作为一个选项,而不是尝试插入where
子句。可能有某种注入方法,但我想不出任何方法。@AdilMammadovGetGroups
在我无法访问的库中。我只有一个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 );