Cannot foreach through data context

眉间皱痕 提交于 2019-12-25 01:53:59

问题


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
    }
}


来源:https://stackoverflow.com/questions/8013493/cannot-foreach-through-data-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!