'Azure function - Method 'All' is not supported in linq query use to query cosmos db

Using the below LINQ query inside Azure function

var query = dbClient.CreateDocumentQuery<MyModel>(collectionUri, feedOptions).
            Where(w => w.ParentColl.All(o => o.Value.ChildColl.Name!= null))
            .AsDocumentQuery();

Above query throws exception

System.Private.CoreLib: Exception while executing function: MyFunction. 
Microsoft.Azure.DocumentDB.Core: Method 'All' is not supported., 
Windows/10.0.19044 documentdb-netcore-sdk/2.11.0.


Solution 1:[1]

First of all, CosmosDB v2 is deprecated and you should upgrade to a supported version. This would also make it easier for yourself to find examples and help, if needed.

Why

Regarding the exception, it is telling you that predicate All(..) cannot be automatically translated to a query so it could be executed on the cosmosDB back end server. Obviously System.Linq can do a lot more tricks than is implemented on CosmosDB v2 legacy library.

Workaround: SQL API

You could write your query using SQL API yourself and avoid the need to translate this predicate at all.. This is the preferred solution, imho.

Fix: LINQ on objects

If you really want LINQ then you should be able to apply such unsupported predicates after you fetch the results. Just apply the predicate to plain c# objects, something along the lines of:

var query = dbClient.CreateDocumentQuery<MyModel>(collectionUri, feedOptions)
    .AsDocumentQuery();

while (query.HasMoreResults)
{
    var results = (await query.ExecuteNextAsync<MyModel>())
        .Where(w => w.ParentColl.All(o => o.Value.ChildColl.Name!= null))
    //Do stuff with results.    
}

DO note though, that you should ensure you are still filtering data on the server enough or you will be hit with bad performance and a huge RU bill.

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 Imre Pühvel