0

Is it possible in mongo db to write a query to access the year portion of a DateTimeOffset

i.e.

var result = from x in coll.AsQueryable()
             where x.MyDate.Year == 2018
             select x;

Can I do this with a custom serializer ? if so how ? and how does Linq handle it? or would I have to cut the query in raw Json ?

Tim Jarvis
  • 18,465
  • 9
  • 55
  • 92
  • 4
    Where MyDate >= 1st Jan 2018 and MyDate < 1st Jan 2019. – mjwills Nov 12 '18 at 06:16
  • 2
    As noted, it's a "date range". Also various examples at [MongoDB .NET Driver Group By Time Range](https://stackoverflow.com/a/50552288/2313887) if the other existing examples are not clear enough. – Neil Lunn Nov 12 '18 at 06:21
  • Tim. They are all duplicates. You cannot "select by **year**" there is only *"date ranges"* just like the existing answers and the good first commenter also said. Read them and learn how it's done. – Neil Lunn Nov 12 '18 at 06:27
  • @NeilLunn actually its only your link in the comment that i'd agree is a duplicate those other 2 not so much - and thanks, its actually your answer (in that link) that helps ;-) – Tim Jarvis Nov 12 '18 at 06:31
  • @mjwills yes that will work I think - I was not wanting to use a range (mainly for readability) but if I have to I have to – Tim Jarvis Nov 12 '18 at 06:32
  • 2
    Let's agree to disagree on that since I think they all say the same thing about the point that matters here. BTW if you find a useful post then there's supposed to be an action for that here. – Neil Lunn Nov 12 '18 at 06:33
  • @NeilLunn - Turns out I had upvoted that a couple weeks ago, when looking for some other issue - If I could upvote twice I would :-) – Tim Jarvis Nov 12 '18 at 06:35

0 Answers0