C
C#3mo ago
TOKYODRIFT!

System.InvalidOperationException: The LINQ expression 'DbSet<WorkingTask>() .Where(w => new Work

I have following code:
[UsePaging]
[UseProjection]
[UseFiltering]
[UseSorting]
[Authorize(Roles = [Permissions.Rms.Task.Read])]
public IQueryable<WorkingTask> WorkingTasks([Service(ServiceKind.Resolver)] ReadModelDbContext dbContext, IResolverContext context, [Service] NpgsqlHealthChecker npgsqlHealthChecker)
{
var workingTasks = dbContext.WorkingTasks
.AsQueryable()
.Project(context)
.Filter(context)
.Sort(context)
.ToList(); // Force client-side evaluation

return workingTasks.AsQueryable();
}
[UsePaging]
[UseProjection]
[UseFiltering]
[UseSorting]
[Authorize(Roles = [Permissions.Rms.Task.Read])]
public IQueryable<WorkingTask> WorkingTasks([Service(ServiceKind.Resolver)] ReadModelDbContext dbContext, IResolverContext context, [Service] NpgsqlHealthChecker npgsqlHealthChecker)
{
var workingTasks = dbContext.WorkingTasks
.AsQueryable()
.Project(context)
.Filter(context)
.Sort(context)
.ToList(); // Force client-side evaluation

return workingTasks.AsQueryable();
}
I don't know why but I always get error after .Sort(context).ToList();. how to fix this?
3 Replies
TOKYODRIFT!
TOKYODRIFT!3mo ago
full error:
The LINQ expression 'DbSet<WorkingTask>()
.Where(w => new WorkingTask{ Identity = w.Identity }
.State == __p_0)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.
The LINQ expression 'DbSet<WorkingTask>()
.Where(w => new WorkingTask{ Identity = w.Identity }
.State == __p_0)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.
but I already use ToList i use HotChocolate
Jimmacle
Jimmacle3mo ago
SQL doesn't know how to create C# objects so new WorkingTask{ Identity = w.Identity } is not something that can be translated into a query it would be fine if you were projecting into it because that's on the C# side after the query runs but you're trying to use that object in a WHERE
TOKYODRIFT!
TOKYODRIFT!3mo ago
this method used by graphql, let's some examples
{
workingTasks(
where: { actualEndTime: {eq: "2024-03-28T14:54:33Z"} }) {
nodes {
identity
}
}
}
{
workingTasks(
where: { actualEndTime: {eq: "2024-03-28T14:54:33Z"} }) {
nodes {
identity
}
}
}
{
workingTasks(
where: { identity: {eq: "123"} }) {
nodes {
identity
}
}
}
{
workingTasks(
where: { identity: {eq: "123"} }) {
nodes {
identity
}
}
}
and if the first one doesn't work at all, the second one works so sometimes it can be translated :OhNo: and I get error after .Filter(context).ToList(), .Project(context).ToList() works good