Cannot foreach through data context

2019-09-12 04:03发布

问题:

i want to compare through 2 information, one is user input and second is admin ID in database. in my project, i'm using WCF Ria. i did created one auto-generated Domain Service Class and the code to retrieve everything in tblAdmin was auto-generated. i load the data in this way ::

        var context = new OrganizationContext();
        var x = context.tblAdmins;
        context.Load(context.GetTblAdminsQuery());
        cb1.ItemsSource = x;

it can load in this way, but i cannot get the x.adminID with this. so i tried this ::

        foreach (var admin in x)
        {
            cb1.Items.Add(admin.adminID);
        }

but failed... may i know is that possible to dig through the data without foreach or is there something wrong in my code ??

回答1:

Looks like the problem is that the context.Load call is asynchronous - to get the result you need to pass a callback and get your data there:

context.Load(context.GetTblAdminsQuery(), LoadCompleted, null);

and:

public void LoadCompleted(LoadOperation<YOUR_ENTITY_TYPE> op)
{
    foreach(var item in op.Entities)
    {
        //item is your entity, you can get item.adminID
    }
}