linqxamarin.formsazure-mobile-servicesquery-expressionsnotsupportedexception

Exception: when trying to use Enumerable.Any Method with Mobile Services query


I have a mobile app build with Xamarin Forms and I'm using Offline Data Sync in Azure Mobile Apps, to enable offline sync in my mobile application

so, I have two tables

and I want to sync the records in the PurchaseOrders Table based on UserId

and then sync the records in PurchaseOrderDetails Table based on Id of PurcahseOrder

To do that I'm trying this

Get Purchase Orders based on User Id

var purchaseOrders = (await GetAllPurchaseOrdersAsync(syncFromServer:true, userId)).ToList();

// in sync method 
await PurchaseOrdersTable.PullAsync(queryName, PurchaseOrdersTable.CreateQuery().Where(w => w.Userid == userId));

When I'm trying to get Purchase Order Details based on Id in the list of PurchaseOrders

await PurchaseOrderDetailsTable.PullAsync(queryName,
                         PurchaseOrderDetailsTable.CreateQuery().Where(pod => purchaseOrders.Any(po => po.Id == pod.PoId)));

I get the exception below

'value(System.Collections.Generic.List`1[ProjectName.Models.PurchaseOrder]).Any(po => (po.Id == pod.PoId))' is not supported in a 'Where' Mobile Services query expression.

Can I get an explanation about the error above and how to solve it?


Solution

  • If your mobile data is backed by table service, then Any is not a supported LINQ operator (see: https://learn.microsoft.com/en-us/rest/api/storageservices/query-operators-supported-for-the-table-service) so that might be source of the exception.

    Since PurchaseOrder doesn't contain a collection of PurchaseOrderDetail, one workaround I can think of is to iterate on purchaseOrders and select from purchaseOrderDetails.

    var poDetails = new List<PurchaseOrderDetail>();
    foreach (var po in purchaseOrders)
    {
       var poDetail = await PurchaseOrderDetails.PullAsync(queryName, PurchaseOrderDetailsTable.CreateQuery().Where(pod => po.Id == pod.PoId);
       poDetails.Add(poDetail);
    }