Nemesis
Nemesis

Reputation: 135

Unable to combine two IQueryAbles

I have two queries which are returning the same type of object. I want to combine them before executing (converting to list). However I am getting an error;

What I am trying

var current = context.Type.Include(b => b.Activity);
var pending = context.Type.IgnoreQueryFilter().Include(b => b.Activity).Where(b => !b.accepted);
return current.Concat(pending);

I am getting the following error

ArgumentException: The input sequence must have items of type 'Test.Models.Type', but it has items of type 'Microsoft.EntityFrameworkCore.Query.Internal.AnonymousObject'.

Upvotes: 1

Views: 1362

Answers (2)

Oliver
Oliver

Reputation: 45119

Whenever you start a new query (starting from context.table again) you write a completely new query which has to be executed separately and can only be combined on the client side.

In your case you should check what Where() your context globally adds within OnModelCreating() (hence you use IgnoreQueryFilter()) and taking the logic from there and OR this into your second Where() clause.

Here is an example:

// Your setting of a global file e.g. hides all as deleted marked items
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Type>().HasQueryFilter(b => !b.IsDeleted);
}

// Instead of creating two queries, you have to manually combine both cases into one query
private void DoSomething()
{
    var combined = context.Type.IgnoreQueryFilter().Include(b => b.Activity).Where(b => !b.accepted || !b.IsDeleted);
}

Upvotes: 1

Shahid Manzoor Bhat
Shahid Manzoor Bhat

Reputation: 1335

Can you update this line of code:

var pending = context.Type.IgnoreQueryFilter().Include(b => b.Activity).Where(b => !b.accepted);

To :

var pending = context.Type.IgnoreQueryFilter().Include(b => b.Activity).Where(b => !b.accepted).ToList();

Upvotes: 0

Related Questions