npgsql / efcore.pg

Entity Framework Core provider for PostgreSQL
PostgreSQL License
1.52k stars 223 forks source link

EF.Functions support for json_array_length #3154

Open alastair-todd opened 5 months ago

alastair-todd commented 5 months ago

I could be mistaken but EF.Functions support for JSON appears limited.

I have a dynamic JSON column (no fixed POCO) which is storing an array of objects. Sometimes this is stored as an empty array, and I want to filter out those rows.

This is the working SQL, but I don't seem to be able to attain this using EF.Functions.

SELECT "Id", "Json"
    FROM "Reporting"."ReportSnapshot"
    where jsonb_array_length("Json") > 0;
roji commented 5 months ago

Have you tried using GetArrayLength() as per the docs?

customer.RootElement.GetProperty("Orders").GetArrayLength()
alastair-todd commented 5 months ago

I didn't think it was relevant to my use case as I've gone for "string mapping" over JsonDocument.

I will try it out.