简体   繁体   English

实体框架 - 包含一个 IQuaryable 参数

[英]Entity framework - Include an IQuaryable parameter

I read EF 5 introduced filtered includes.我阅读了 EF 5 引入的过滤包含。

This allows me to write something like this:这允许我写这样的东西:

db
  .MyCollection
  .Include(mc => 
     mc
      .MySubCollection
      .Where(msc => /* Some condition */)
  )

But I'd like to handle the .Where(msc => /* Some condition */) part in a separate function to make my filtering more generic (wip).但我想在单独的 function 中处理.Where(msc => /* Some condition */)部分,以使我的过滤更通用(wip)。

So I thought my separate function could look like:所以我认为我单独的 function 可能看起来像:

private IQueryable<MySubCollection> FilterSubCollection(IQueryable<SubCollection> source)
{
  //Some filter operations
  //This is just for example, filters could be (x > y), (x == y), etc.
  foreach(/* filterFieldValues */)
    source = ApplyFilter(source);

  return source;
}

And call the method like:并像这样调用方法:

db
  .MyCollection
  .Include(mc => 
     FilterSubCollection(mc.MySubCollection)
  )

The problem is that the Include() Method can't handle the IQueryable object and gives me this warning at compile time: 'Cannot convert lambda expression to type 'string' because it is not a delegate type'.问题是Include()方法无法处理 IQueryable object 并在编译时向我发出此警告:“无法将 lambda 表达式转换为类型‘字符串’,因为它不是委托类型”。

Fact is I can copy-paste all the conditions applied in my method FilterSubCollection and paste them in the include to make it work, but i'm working on a more generic solution which is why I'm trying to work with IQueryable.事实上,我可以复制粘贴我的方法FilterSubCollection中应用的所有条件并将它们粘贴到包含中以使其工作,但我正在研究一个更通用的解决方案,这就是我尝试使用 IQueryable 的原因。

Is there any way to make the Include() method accept an IQueryable<> ?有什么方法可以使Include()方法接受IQueryable<>吗?

So the answer is I got mislead by the EF versions.所以答案是我被 EF 版本误导了。 EF 5 actually stands for EF Core 5 which is more recent than the EF 6 which does not allow such operation (afaik). EF 5 实际上代表 EF Core 5,它比不允许此类操作的 EF 6 更新(afaik)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM