Home > Blockchain >  Entity framework - Include an IQuaryable parameter
Entity framework - Include an IQuaryable parameter

Time:01-27

I read EF 5 introduced filtered includes.

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).

So I thought my separate function could look like :

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'.

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.

Is there any way to make the Include()method accept an IQueryable<> ?

CodePudding user response:

So the answer is I got mislead by the EF versions. EF 5 actually stands for EF Core 5 which is more recent than the EF 6 which does not allow such operation (afaik).

  • Related