将 WCF RIA 服务查询结果加载到 ObservableCollection

发布于 2024-10-01 13:46:29 字数 722 浏览 0 评论 0原文

在我的 Silverlight 应用程序中,创建 ADO.NET 实体数据模型和 WCF RIA 服务域服务类后,在相应的 ProductService 类中,我有一个查询操作,该操作将 Product 实体的集合返回给客户端,如下所示:

public IQueryable<Product> GetProducts()
{
   return this.ObjectContext.Products;
}

现在我正在尝试在客户端 Silverlight 应用程序中读取它并将结果加载到 ObservableCollection:

ProductContext pcontext = new ProductContext();
ObservableCollection<Prod> prAvs = pcontext.GetProductsQuery();

但出现错误:

无法隐式转换类型 System.ServiceModel.DomainServices.Client.EntityQuerySystem.Collections.ObjectModel.ObservableCollection

我该如何解决该问题?

In my Silverlight app, after creating ADO.NET Entity Data Model and WCF RIA Services Domain Service Class, in a corresponding ProductService class I have a query operation that returns a collection of Product entities to the client, as follows:

public IQueryable<Product> GetProducts()
{
   return this.ObjectContext.Products;
}

Now I'm trying to read it in the client Silverlight app and load results to an ObservableCollection:

ProductContext pcontext = new ProductContext();
ObservableCollection<Prod> prAvs = pcontext.GetProductsQuery();

But getting an error:

Cannot implicitly convert type System.ServiceModel.DomainServices.Client.EntityQuery<MyTestApp.Web.Product>
to System.Collections.ObjectModel.ObservableCollection<MyTestApp.Prod>

How could I fix that issue?

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

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

发布评论

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

评论(1

心清如水 2024-10-08 13:46:29

第一个问题:

您应该使用 RIA 服务为您生成的客户端 Products 类,而不是您自己定义的另一个类。

例如,您应该拥有 MyTestApp.Web.Product 的集合,而不是 MyTestApp.Prod 对象的集合。

您将在客户端项目中隐藏的“Generate_Code”文件夹中找到生成的域上下文。其中将包含一个 MyTestApp.Web.g.cs 文件,其中包含客户端上下文和任何数据对象(如 MyTestApp.Web.Product)。

第二个问题:

您不能只将查询转换为集合。

您需要使用查询来加载实体更改集。

var loadOperation = pcontext.Load(pcontext.GetProductsQuery());

结果(加载完成时)是返回的 loadOperation 对象中的实体集合。您可以立即使用实体集合,但它最初是空的。

First problem:

You should be using the client-side Products class generated for you by RIA services, not another class you define yourself.

e.g you should have a collection of MyTestApp.Web.Product, not of MyTestApp.Prod objects.

You will find the generated domain context in a hidden Generated_Code folder in your client project. Within that will be a MyTestApp.Web.g.cs file containing the client side context and any data objects (like MyTestApp.Web.Product).

Second issue:

You can't just cast a query to a collection.

You need to use the query to load an entity change-set instead.

var loadOperation = pcontext.Load(pcontext.GetProductsQuery());

The result (when the load completes) is an entity collection in the returned loadOperation object. You can use the entity collection immediately, but it is initially empty.

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