【问题标题】:How can I join data with Linq to Entities and WCF Data Services?如何使用 Linq to Entities 和 WCF 数据服务加入数据?
【发布时间】:2012-01-30 00:52:48
【问题描述】:

我有 4 个相关的实体如下:

LocalAgency<-0..1----1->Agency<-0..1----1->Organization<-0..1----1->Customer

换句话说,LocalAgency 有一个相关的Agency 等。数据模型是使用Entity Framework 设置的(包含用于细读这些关系的导航属性),并且 WCF DataService 设置为将这些数据提供给客户。

在使用DataService 的客户端上,我尝试根据客户名称返回本地代理机构的查询,但没有找到支持的方式来制定这个简单的查询。

我尝试的第一种方法是使用Expand,如下:

var items = (from i in Context.LocalAgencies.Expand("Agency").Expand("Organization").Expand("Customer")
             where (String.IsNullOrEmpty(CustomerName) || i.Agency.Organization.Customer.CustomerName.Contains(CustomerName))
             select i).Skip(StartIndex).Take(PageSize).ToList<LocalAgency>();

如果“连接”只有 1 级深度,则此方法有效,但无法获取导航属性的导航属性。

然后我尝试了join,如下所示:

var items = (from localAgency in Context.LocalAgencies
             join agency in Context.Agencies on localAgency.CustomerID equals agency.CustomerID
             join organization in Context.Organizations on localAgency.CustomerID equals organization.CustomerID
             join customer in Context.Customers on localAgency.CustomerID equals customer.CustomerID
             where (String.IsNullOrEmpty(CustomerName) || customer.CustomerName.Contains(CustomerName))
             select localAgency).Skip(StartIndex).Take(PageSize).ToList<LocalAgency>();

但是,join 在此实例中不受支持。

然后我尝试使用Except 方法如下:

IQueryable<LocalAgency> items = Context.LocalAgencies;
items = items.Except(from i in items
                     where (String.IsNullOrEmpty(CustomerName) || i.Agency.Organization.Customer.CustomerName.Contains(CustomerName))
                     select i).Skip(StartIndex).Take(PageSize);

但是,Except 在此实例中不受支持。

我错过了什么?我是否需要在 DataService 端设置一些东西以允许沿定义的导航属性进行简单连接?

【问题讨论】:

    标签: c# linq entity-framework linq-to-entities wcf-data-services


    【解决方案1】:

    我在Expand 上使用了错误的语法。我做了以下事情:

    var items = (from i in Context.LocalAgencies.Expand("Agency").Expand("Agency/Organization").Expand("Agency/Organization/Customer")
    where (String.IsNullOrEmpty(CustomerName) || i.Agency.Organization.Customer.CustomerName.Contains(CustomerName))
    select i).Skip(StartIndex).Take(PageSize).ToList<LocalAgency>();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      • 2011-07-24
      • 1970-01-01
      相关资源
      最近更新 更多