zzzprojects / System.Linq.Dynamic.Core

The .NET Standard / .NET Core version from the System Linq Dynamic functionality.
https://dynamic-linq.net/
Apache License 2.0
1.56k stars 228 forks source link

Example in Documentation is not working in Blazor Server: Expression with DateTime #616

Closed Mathieu333 closed 2 years ago

Mathieu333 commented 2 years ago

1. Description

I have a blazor server project and I am using Linq.Dynamic.Core 1.2.19 and Microsoft.EntityFrameworkCore 6.0.7. So far so good a lot of the code is working great, many thanks !

image

The below code throws the exception.

using (var context = new EntityContext())
        {
            contractList = context.Contracts.Where("DateC >= DateTime(2023, 1, 1)").ToList();
        }

It is apparently the same line of code as the documentation yet it does not work. The contractList is a list of Contract that can't be simpler:

public class Contract
    {
        [Key]  public int? UId { get; set; }
        public DateTime? DateC { get; set; }

    }

The link to the database is working and the Contract table is matching the Contract class. EntityContext is working as expected.

2. Exception

Conversion failed when converting date and/or time from character string.

3. Fiddle or Project

Simple project attached, BlazorApp1.zip

you just have to change the connectionString in the EntityContext to your own database and create a very simple contract table 👍 image

Mine is populated like so. image

Disclaimer: I am aware that the https://dotnetfiddle.net/BWGuvY test project is working as expected. I have spend a lot of time running this project from scratch and testing things around. It does not work on my computer but as you can see the project attached is very simple.

Is it just that it will not work with Blazor Server? Or could that be DateTime? ? I have similar result with the non-nullable type.

Thank you for your help !

StefH commented 2 years ago

@Mathieu333 Can you provide the full exception?

Because I did import your project (slightly modified some code) and tested it, it works fine.

You can see the PR: https://github.com/zzzprojects/System.Linq.Dynamic.Core/pull/618

Mathieu333 commented 2 years ago

@StefH First of all thank you very much for taking the time to test it !

Here is the full exception.

fullException.zip

I am really surprised it works on your side. Will take a look at that PR thanks.

Mathieu333 commented 2 years ago

@StefH I tried your PR with <PackageReference Include="System.Linq.Dynamic.Core" Version="1.2.19" /> instead of the ProjectReference and it still throws the exception :

Internal Server Error.zip

Maybe it is related to some sort of globalization culture issue if it is working on your side but not mine?

StefH commented 2 years ago

Yes. Probably Globalization or Culture.

What do you use for your system?

Mathieu333 commented 2 years ago

My InstalledUICulture is fr-FR. I have tried to set up CurrentCulture and CurrentUICulture to en-US before the expression but still no luck.

StefH commented 2 years ago

Mine is this: image

I'll try if I can change it

StefH commented 2 years ago

After changing the culture as defined here: https://docs.microsoft.com/en-us/aspnet/core/blazor/globalization-localization?view=aspnetcore-6.0&pivots=server#statically-set-the-culture

It still works: image

Mathieu333 commented 2 years ago

Thank you for your help.

I guess I am back to square one, I'll probably try a full reinstall of a clean windows on one of my comp tomorrow..

Mathieu333 commented 2 years ago

I did some searching and actually this help me :

https://github.com/dotnet/efcore/issues/14095#issuecomment-464365442

modelBuilder.Entity<TestEntity>().Property(x => x.LastModified).HasColumnType("datetime");

With this the code works indeed, it has nothing to do with Linq.Dynamic.Core and probably comes from datetime2 being the standard in EF Core.

Many thanks @StefH you are a life saver ! 🥇