无法通过数据上下文进行 foreach [英] Cannot foreach through data context
问题描述
我想通过 2 个信息进行比较,一个是用户输入,第二个是数据库中的管理员 ID.在我的项目中,我使用的是 WCF Ria.我确实创建了一个自动生成的域服务类,并且在 tblAdmin 中检索所有内容的代码是自动生成的.我以这种方式加载数据::
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;
它可以以这种方式加载,但我无法通过此方式获取 x.adminID.所以我试过这个 ::
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);
}
但是失败了...我是否知道可以在没有 foreach 的情况下挖掘数据,或者我的代码有问题吗??
but failed... may i know is that possible to dig through the data without foreach or is there something wrong in my code ??
推荐答案
看起来问题是 context.Load 调用是异步的 - 要获得结果,您需要传递回调并在那里获取数据:
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);
和:
public void LoadCompleted(LoadOperation<YOUR_ENTITY_TYPE> op)
{
foreach(var item in op.Entities)
{
//item is your entity, you can get item.adminID
}
}
这篇关于无法通过数据上下文进行 foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!