如何使用Linq将数据联接到实体和WCF数据服务? [英] How can I join data with Linq to Entities and WCF Data Services?
问题描述
我有4个与以下内容相关的实体:
I have 4 entities that are related as follows:
LocalAgency<-0..1----1->Agency<-0..1----1->Organization<-0..1----1->Customer
换句话说,LocalAgency
具有一个相关的Agency
,依此类推.使用Entity Framework
(包含导航属性以细读那些关系)来建立数据模型,并且将WCF DataService
设置为将这些数据提供给客户.
In other words, a LocalAgency
has one related Agency
, etc. The data model is set up using Entity Framework
(containing navigation properties to peruse those relationships), and a WCF DataService
is set up to provide that data to clients.
在使用DataService
的客户端上,我试图基于客户名称返回对本地代理商的查询,但是还没有找到支持该简单查询的方法.
On the client side that consumes the DataService
, I'm trying to return a query of local agencies based on a customer name, but haven't found a supported way to formulate this simple query.
我尝试的第一种方法是使用Expand
,如下所示:
The first method I tried was using Expand
as follows:
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>();
如果"join"仅深1级,则此方法有效,但这无法获得导航属性的导航属性.
This approach works if the "join" is only 1 level deep, but this fails to get navigation properties of navigation properties.
然后我尝试了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
.
But, join
is not supported in this instance.
然后我尝试使用Except
方法,如下所示:
I then tried using the Except
method as follows:
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
.
But, Except
is not supported in this instance.
我想念什么?我是否需要在DataService
端进行一些设置,以允许沿着定义的导航属性进行简单连接?
What am I missing? Do I need to set up something on the DataService
side to allow a simple join along defined navigation properties?
推荐答案
我在Expand
上使用了错误的语法.我做了以下事情:
I used the wrong syntax on the Expand
. I did the following:
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>();
这篇关于如何使用Linq将数据联接到实体和WCF数据服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!