0

I have a model that defines field date_sk in Cosmos Db database, and correspondent DateSk in code:

    public class TestEntity { ...
       [JsonPropertyName("date_sk")]
       public Date DateSk { get; set; }
   }

I have no issue with running query like SELECT * FROM c where c.date_sk='2011-11-11' in Cosmos Db explorer, or as a raw query in c# like this:

   FeedIterator<TestEntity> feeder = GetContainer().GetItemQueryIterator<TestEntity>(
                    new QueryDefinition($"SELECT * FROM c where c.date_sk='2011-11-11'"));

However when I try to run LINQ alternative:

var feeder = GetContainer()
                .GetItemLinqQueryable<TestEntity>()
                .Where(f => f.DateSk == "2021-11-11");

it throws no error, but nothing returned back, and when I put a breakpoint to see SQL query that runs behind scene, I can see SELECT VALUE root FROM root WHERE (root["DateSk"] = "2021-11-11"). It shows DateSk property name used f;at straight instead of date_sk real field name set with JsonPropertyName attribute. Interestingly the mapping works fine if I use other than LINQ queries, at least data is properly transferred one direction date_sk->DateSk, so the JsonPropertyName attribute works fine in the rest of situations

Any idea what could be wrong?

YMC
  • 4,925
  • 7
  • 53
  • 83
  • Is there a compelling reason to use Linq here, though? – Dai Nov 14 '21 at 17:42
  • I found some posts from 2016 of people complaining of the same thing: that CosmosDB's Linq provider does not respect JSON property names (either `Newtonsoft.Json` or `System.Text.Json`): https://stackoverflow.com/questions/37489768/how-to-tell-documentdb-sdk-to-use-camelcase-during-linq-query/37490316#37490316 - sucks – Dai Nov 14 '21 at 17:44
  • Also https://github.com/Azure/azure-cosmos-dotnet-v2/issues/317 – Dai Nov 14 '21 at 17:44
  • 1
    it's simplified example, using LINQ is preferable for maintainability reason (less hardcode, strongly typed). Thank you links you provided, they are all about Newtonsoft Json serializer, I've been using different, namespace System.Text.Json, one that Microsoft moving toword – YMC Nov 14 '21 at 18:25
  • btw I'm setting serializer setting to camel case (PropertyNamingPolicy = JsonNamingPolicy.CamelCase) like these links recommend, it does not help – YMC Nov 14 '21 at 18:37
  • I believe Newtonsoft is used internally by the SDK. If you're using `JsonPropertyName` from System.Text.Json, that won't be respected by SDK. This among other reasons is why I became happier after I persuadd myself to get away from LINQ and use `QueryDefinition` instead. Entire classes of confusion, avoided. – Noah Stahl Nov 14 '21 at 21:37
  • Well, seems like Microsoft simply does not support LINQ conversion in its System.Text.Json https://social.msdn.microsoft.com/Forums/vstudio/en-US/f6d33682-a02a-4d9e-b3af-470fa3f7836c/linq-not-recognizing-jsonpropertyname-when-using-systemtextjson?forum=csharpgeneral, Newtsonsoft.Json works though – YMC Nov 22 '21 at 06:45

0 Answers0