1
我有4个被相关实体如下:如何将数据与Linq连接到实体和WCF数据服务?
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
一侧设置一些内容以允许沿定义的导航属性进行简单连接?