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

How to GroupBy Nullable DateTime Year? #424

Closed
brainded opened this issue Sep 30, 2020 · 9 comments
Closed

How to GroupBy Nullable DateTime Year? #424

brainded opened this issue Sep 30, 2020 · 9 comments
Labels

Comments

@brainded
Copy link

I have tried the following to get the group by to work for this but I am having trouble getting past the nullable issues.

.GroupBy("new (NullableDate.Year)") throws an exception because the Year property doesn't exist. Makes sense because the property is actually on Value.
.GroupBy("new (NullableDate?.Year)") throws an exception that you can't use an NP with the library. I found the exception in the source code and I kinda get why that isn't supported.
.GroupBy("new (NullableDate.Value.Year)") works! But then fails to materialize results because the value returned should be nullable.

Is there a supported way to do what I am trying to do here? Or even an unsupported workaround?

@StefH
Copy link
Collaborator

StefH commented Sep 30, 2020

@brainded
You could take a look at null propagation : https://dynamic-linq.net/advanced-null-propagation

@brainded
Copy link
Author

@StefH Thank you!

I tried:
.GroupBy("new (np(NullableDate.Year, 0))") and got "No property or field 'Year' exists in type 'DateTime?" again.
I also tried:
.GroupBy("new (np(NullableDate.Value.Year))")
and
.GroupBy("new (np(NullableDate.Value.Year, 0))")
and got "The binary operator NotEqual is not defined for the types 'System.DateTime' and 'System.Object'."

Is NP supported in the GroupBy method? Or just in the Where and Select methods? Seems like the examples using the NP operator are Objects with a property.... but this is a little different being a Nullable base type.

@StefH
Copy link
Collaborator

StefH commented Nov 29, 2020

Hello @brainded, I'll investigate.

@StefH StefH added the bug label Nov 29, 2020
@zspitz
Copy link

zspitz commented Nov 29, 2020

@StefH Might I suggest the following? Ordinarily, np rewrites a member chain:

np(PropA.PropB.PropC)

into a ternary conditional:

(PropA != null && PropB != null) ? PropA.PropB.PropC : null

But if any of the property checks (PropA or PropB) return a nullable struct, the rewrite should be as follows:

(PropA.HasValue && PropA.Value.PropB.HasValue) ? PropA.Value.PropB.Value.PropC : null

This assumes it would be legal syntax to write:

PropA.PropB.PropC

even if PropA and PropB return nullable structs, and should be translated to :

PropA.Value.PropB.Value.PropC

@StefH
Copy link
Collaborator

StefH commented Dec 1, 2020

Hello @brainded

Can you please test NuGet 1.2.7-preview-02 from https://www.myget.org/F/system-linq-dynamic-core/api/v3/index.json ?

@StefH
Copy link
Collaborator

StefH commented Dec 5, 2020

Dear @brainded, did you have time to test this?

@StefH
Copy link
Collaborator

StefH commented Dec 9, 2020

Dear @brainded, did you have time to test this?

@StefH
Copy link
Collaborator

StefH commented Dec 12, 2020

Dear @brainded,

I'll close this issue and merge to fix to master. A new version will be released within a week.

In case you still have questions or issues with the new release, just open a new Issue here.

@StefH StefH closed this as completed Dec 12, 2020
@brainded
Copy link
Author

@StefH finally getting some time to return back to this after the holidays. updating now and will report back! Thank you for looking into it!

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

No branches or pull requests

3 participants