简体   繁体   中英

MobileServiceInvalidOperationException Xamarin.Forms connecting to Azure using LINQ

I have database on azure with tables Conferences and Sessions. Session has foreign key to one conference. In front-end I'm trying to connect to this database and get sessions with foreign key to specific conference, but when I try to do this, I get following problem

Exception

My code:

 // Return Sessions of the current conference
    public async Task<ObservableCollection<Session>> GetSessionsAsync(Conference currentConference, bool syncItems = false)
    {
        try
        {
            IEnumerable<Session> sessions = await sessionTable
//problem is here           .Where(s => s.ConferenceId == currentConference.Id)
                            .ToEnumerableAsync();
            return new ObservableCollection<Session>(sessions);
        }
        catch (MobileServiceInvalidOperationException msioe)
        {
            Debug.WriteLine(@"MSIOE exception: {0}", msioe.Message);
        }
        catch (Exception e)
        {
            Debug.WriteLine(@"Some exception: {0}", e.Message);
        }
        return null;
    }

The most interesting thing is that if I write

Debug.WriteLine(sessions.First().ConferenceId == currentConference.Id);

after the problem string (it works without LINQ "Where"), this will show "true".

Even if I use linq expressions after the problem string they work well. And if I use where clause not with column ConferenceId it also works for me.

PS In Debug windows I see MSIOE exception

It's generally good practice, when you're doing a Linq to Entities query, to minimise your use of property accessors within the query itself. Try this.

public async Task<ObservableCollection<Session>> GetSessionsAsync(Conference currentConference, bool syncItems = false)
{
    try
    {
        var conferenceId = currentConference.Id;
        IEnumerable<Session> sessions = await sessionTable
                        .Where(s => s.ConferenceId == conference.Id)
                        .ToEnumerableAsync();
        return new ObservableCollection<Session>(sessions);
    }
    catch (MobileServiceInvalidOperationException msioe)
    {
        Debug.WriteLine(@"MSIOE exception: {0}", msioe.Message);
    }
    catch (Exception e)
    {
        Debug.WriteLine(@"Some exception: {0}", e.Message);
    }
    return null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM