RIA服务查询:嵌套“来自”并包含子项

时间:2010-04-02 07:03:51

标签: linq wcf-ria-services

我有以下schema

我想要做的是从StaffId的所选StaffAssignment中检索所有客户(和子属性)。

所以我写了以下查询:

from c in ObjectContext.Customers.Include("Assignments.Activities")
               from a in c.Assignments
               from sa in a.StaffAssignments
               where sa.StaffId == staffId
               select c

但是没有加载子属性(我在服务元数据文件中也添加了[Include])。

我做错了什么?

GTZ, 斯特凡。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。

这是我的模型(取自NorthWind)

alt text http://i33.tinypic.com/5k0g3n.png

DomainService 如下所示:

[EnableClientAccess()]
public class DomainService1 : LinqToEntitiesDomainService<AdventureWorksLT2008Entities>
{
    public IQueryable<Customer> GetCustomer()
    {
        return this.ObjectContext.Customer.Include("CustomerAddress").Include("CustomerAddress.Address");
    }
}

我在所有导航属性中添加了[Include]属性:

public partial class Customer
{
    internal sealed class CustomerMetadata
    {
        // Metadata classes are not meant to be instantiated.
        private CustomerMetadata() { }

        public string CompanyName { get; set; }

        [Include]
        public EntityCollection<CustomerAddress> CustomerAddress { get; set; }


在客户端代码中,我使用了这个:

var dc = new MyDomainContext();

var query = dc.GetCustomerQuery();

var op = dc.Load(query, loadOperation =>
{
    var customer = loadOperation.Entities.FirstOrDefault();
    var address = customer.CustomerAddress.FirstOrDefault().Address;
}, null);


请注意,这仅适用于:

  • ObjectContext由EntityFramework(无自定义Context或Poco实体)自动生成
  • DomainService是'LinqToEntitiesDomainService'

另见this link