0
votes

I have a Azure Mobile Apps Xamarin.Forms PCL client and have Offline Sync enabled. I tried to Pull data from my backend and afterwards query data from the offline storage with a Where clause. That throws the following exception and I don't know why.

Sync error: 'fahrerinfo.Imei.Equals("02032032030232")' is not supported in a 'Where' Mobile Services query expression.

    public async Task SyncAsync()
    {

        ReadOnlyCollection<MobileServiceTableOperationError> syncErrors = null;

        try
        {
             await OfflineSyncStoreManager.Instance.TruckFahrerTable.PullAsync("allTruckFahrerItems",
                OfflineSyncStoreManager.Instance.TruckFahrerTable.CreateQuery());

            Debug.WriteLine("SyncAsync: PUSH/PULL completed.");

        }
        catch (MobileServicePushFailedException e)
        {
            Debug.WriteLine("SyncAsync: PUSH failed.");
            Debug.WriteLine(e.Message);
        }
        catch (Exception e)
        {
            Debug.WriteLine("SyncAsync: PUSH/PULL failed.");
            Debug.WriteLine(e.Message);
            //Debugger.Break();
        }
    }

    public async Task<ObservableCollection<TruckFahrer>> GetTruckFaherAsync(bool syncItems)
    {
        try
        {
            if (syncItems)
            {
                await OfflineSyncStoreManager.Instance.SyncAsync().ConfigureAwait(false);
            }

            var deviceInfo = DependencyService.Get<IDeviceInfo>().GetPhoneInfo();
            var imeiString = deviceInfo[trucker_rolsped.PhoneInfo.PhoneInfo.ImeiKey];

            var imei = imeiString.Equals("000000000000000") ? deviceInfo[trucker_rolsped.PhoneInfo.PhoneInfo.IdKey] : imeiString;

            IEnumerable<TruckFahrer> items = 
                await OfflineSyncStoreManager.Instance.TruckFahrerTable
                //.Where(fahrerinfo => fahrerinfo.Imei.Equals(imei)) TODO: Why does that throw an exception???
                .ToEnumerableAsync();

            // TODO: Because above does not work
            items = items.Where(fahrer => fahrer.Imei.Equals(imei));

            return new ObservableCollection<TruckFahrer>(items);
        }
        catch (MobileServiceInvalidOperationException msioe)
        {
            Debug.WriteLine(@"Invalid sync operation: {0}", msioe.Message);
            Debugger.Break();
        }
        catch (Exception e)
        {
            Debug.WriteLine(@"Sync error: {0}", e.Message);
            Debugger.Break();
        }

        return null;
    }

Thanks for any hint,

Eric

1

1 Answers

1
votes

Are you a Java developer too? I'm and had this issue because in Java we need to compare strings with String#equals method, haha.

For some reason MobileServices doesn't allow us to use Equals in this situation.

To fix your problem, use == instead. As you can see here C# difference between == and Equals() both have the same effect in this case.

Where(fahrerinfo => fahrerinfo.Imei == imei)