-1

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<> ?

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
Axel Samyn
  • 160
  • 1
  • 12
  • 1
    Note that EF 5 which has introduced filtered includes is EF Core 5. EF6 is tag for older version of EF for .NET Framework. – Guru Stron Jan 16 '23 at 11:34
  • Oh, I didn't know ! That might be the problem, let me check my version exactly – Axel Samyn Jan 16 '23 at 11:37
  • You were right, my version is EF6. Unfortunately, I cannot upgrade for the moment. I'll just go without using a seperate function for the moment. – Axel Samyn Jan 16 '23 at 12:47

1 Answers1

0

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

Axel Samyn
  • 160
  • 1
  • 12