无法通过数据上下文进行 foreach

发布于 2024-12-13 12:04:04 字数 547 浏览 5 评论 0原文

我想通过 2 条信息进行比较,一是用户输入,二是数据库中的管理员 ID。在我的项目中,我使用 WCF Ria。我确实创建了一个自动生成的域服务类,并且用于检索 tblAdmin 中所有内容的代码是自动生成的。我以这种方式加载数据::

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

它可以以这种方式加载,但我无法用这种方式获取x.adminID。所以我尝试了这个 ::

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

但失败了...我是否可以知道是否可以在没有 foreach 的情况下挖掘数据,或者我的代码中是否有问题?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够运 2024-12-20 12:04:04

看起来问题在于 context.Load 调用是异步的 - 要获取结果,您需要传递回调并在那里获取数据:

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

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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文