pnpcore icon indicating copy to clipboard operation
pnpcore copied to clipboard

Unable to query nullable fields via LINQ expressions

Open SPWizard01 opened this issue 5 months ago • 0 comments

Category

  • [x] Bug

Describe the bug

i.e. you want to query Site Pages for anything that has been published straight away or scheduled time is less than now will throw an error:

System.NotSupportedException: Node of type Equal of expression ((c.Values.get_Item("OData__PublishStartDate") As Nullable`1) == null) is not supported

Steps to reproduce

this works:

            var items = await myList.Items.Where(c =>
                (c.Values["OData__PublishStartDate"] as DateTimeOffset?) <= now
            ).ToListAsync();

this does not:

            var items = await myList.Items.Where(c =>
                (c.Values["OData__PublishStartDate"] as DateTimeOffset?) <= now ||
                (c.Values["OData__PublishStartDate"] as DateTimeOffset?) == null
            ).ToListAsync();

Expected behavior

the OData Query that is produced should be

OData__PublishStartDate le datetime'2024-09-15T05:18:54Z' or OData__PublishStartDate eq null

Environment details (development & target environment)

  • SDK version: 1.14
  • OS: Win 11
  • SDK used in: ASP.Net Web app
  • Framework: .NET Core v8
  • Browser(s): Chromium Edge Latest
  • Tooling: Visual Studio 2022

Additional context

In my opinion the problem is here: https://github.com/pnp/pnpcore/blob/3592355b95fcdad5b0b1f4763768e7b18edf21d7/src/sdk/PnP.Core/QueryModel/Query/DataModelQueryTranslator.cs#L216

I am not sure if null check should be performed on the value.

SPWizard01 avatar Sep 15 '24 18:09 SPWizard01