将 WCF RIA 服务查询结果加载到 ObservableCollection
在我的 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.EntityQuery
到 System.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个问题:
您应该使用 RIA 服务为您生成的客户端
Products
类,而不是您自己定义的另一个类。例如,您应该拥有 MyTestApp.Web.Product 的集合,而不是 MyTestApp.Prod 对象的集合。
您将在客户端项目中隐藏的“Generate_Code”文件夹中找到生成的域上下文。其中将包含一个 MyTestApp.Web.g.cs 文件,其中包含客户端上下文和任何数据对象(如 MyTestApp.Web.Product)。
第二个问题:
您不能只将查询转换为集合。
您需要使用查询来加载实体更改集。
结果(加载完成时)是返回的 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.
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.