Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.InvalidOperationException: The LINQ expression could not be translated. #119

Open
marrrschine opened this issue Feb 25, 2021 · 4 comments

Comments

@marrrschine
Copy link

Hey,
I'm not sure if it's a bug or I'm just boneheaded.
Anyway, when trying to sort a nested object by date, I run into the following exception:

System.InvalidOperationException: The LINQ expression 'DbSet<Job>()
    .OrderBy(j => (object)j.Created == null ? 01/01/0001 00:00:00 : j.Created.At)' 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'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

The API controller method is pretty much like in the SieveTests (but returning new JsonResult instead of Json since a plain Web API controller should inherit from ControllerBase and not from Controller)

[HttpGet]
public JsonResult GetJobs([FromQuery] SieveModel sieveModel)
{
    var result = _context.Jobs.AsNoTracking();
    result = _jobProcessor.Apply(sieveModel, result);
    return new JsonResult(result.ToList());
}

The respective API call is

curl -X GET "http://localhost:5500/api/Jobs?Sorts=sort_by_nested_property" -H "accept: text/plain"

Class JobProcessor added as scoped service in Startup.cs

public class JobProcessor : SieveProcessor
{
    public JobProcessor(
        IOptions<SieveOptions> options,
        ISieveCustomFilterMethods customFilterMethods) : base(options, customFilterMethods)
        { }

    protected override SievePropertyMapper MapProperties(SievePropertyMapper mapper)
    {
        mapper.Property<Job>(p => p.Created.At)
        .CanSort()
        .HasName("sort_by_nested_property");

        return mapper;
    }
}

Class Job

public class Job
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public Status Created { get; set; }
}

Class Status

public class Status
{
    public string State { get; set; }
    public string By { get; set; }
    public DateTime At { get; set; }
}
$ dotnet --version
5.0.102

Does anyone have an idea what is wrong here?

@maulik-modi
Copy link

@marrrschine , Do you mind sharing Datamodel?

@marrrschine
Copy link
Author

which one do you mean specifically @maulik-modi?

@maulik-modi
Copy link

EF DataContext

@marrrschine
Copy link
Author

public class JobContext : DbContext
    {
        public JobContext(DbContextOptions<JobContext> options) : base(options)
        {    
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            // Configure model relations
            builder.ApplyConfiguration(new JobConfiguration());
        }

        public DbSet<Job> Jobs { get; set; }
    }
public class JobConfiguration : IEntityTypeConfiguration<Job>
    {
        public void Configure(EntityTypeBuilder<Job> builder)
        {
            builder
                .Property(x => x.Id)
                .ValueGeneratedOnAdd();

            builder
                .Property(x => x.Name)
                .HasMaxLength(100);

            builder
                .Property(x => x.Status)
                .HasConversion(
                    c => JsonConvert
                        .SerializeObject(c, new JsonSerializerSettings
                        {
                            NullValueHandling = NullValueHandling.Ignore
                        }),
                    c => JsonConvert
                        .DeserializeObject<IList<Status>>(c, new JsonSerializerSettings
                        {
                            NullValueHandling = NullValueHandling.Ignore
                        })
                );
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants