'ASP.NET Core OData - filter on datetime with milliseconds not working?

I'm using Microsoft.AspNetCore.OData version 7.5.14.

I'm trying to execute something like this:

"$filter=cast(START_TIME, Edm.DateTimeOffset) eq 2022-04-13T13:55:55.107Z"

(or ge, or le, for that matter)

Because of the milliseconds, this fails. That date exists in the database, yet I get 0 records returned.

What am I missing here?

EDIT: More information:

I can query that value in SQL Server Mgmt Studio. No problem.

I can use this odata:

"$filter=START_TIME eq 2022-04-13T13:55:55.107-04:00"

-4:00 is the UTC of the server.

The above fails (cast to DateTimeOffset) as it's using this precision:

"$filter=cast(START_TIME, Edm.DateTimeOffset) eq 2022-04-13T13:55:55.1070000Z"

Which won't match the database value. I wanted to use DateTimeOffset as it adds time zone information relative to UTC.



Solution 1:[1]

There's only one mention of the word "memory" in the entire etree documentation, and it's in the context of iterparse() not reading the whole XML into memory:

If you don’t mind your application blocking on reading XML data but would still like to have incremental parsing capabilities, take a look at iterparse(). It can be useful when you’re reading a large XML document and don’t want to hold it wholly in memory.

However, the docs don't show any example of how to use iterparse. PyMOTW 3 has a really good write-up on the entire etree module and includes an example of iterparse.

Without any more context on what do_something() does, the simplest implementation for you would look like:

from xml.etree import ElementTree as ET

for _, elem in ET.iterparse('input.xml'):
    do_something(elem)

Solution 2:[2]

Use the .iterparse function. Parse large XML files incrementally. This is good for large XML files where you don't have to load the whole file into memory.

From the documentation:

xml.etree.ElementTree.iterparse(source, events=None, parser=None)

Parses an XML section into an element tree incrementally, and reports what’s going on to the user. source is a filename or file object containing XML data. events is a sequence of events to report back. The supported events are the strings "start", "end", "comment", "pi", "start-ns" and "end-ns" (the “ns” events are used to get detailed namespace information). If events is omitted, only "end" events are reported. parser is an optional parser instance. If not given, the standard XMLParser parser is used. parser must be a subclass of XMLParser and can only use the default TreeBuilder as a target. Returns an iterator providing (event, elem) pairs.

It's a blocking function, so it's not asychronous. If you have several XML readers at the same time:

Note that while iterparse() builds the tree incrementally, it issues blocking reads on source (or the file it names). As such, it’s unsuitable for applications where blocking reads can’t be made. For fully non-blocking parsing, see XMLPullParser.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zach Young
Solution 2 MmBaguette