如何将Linq与实体和WCF数据服务的数据连接起来?
我有4个相关的实体如下:
LocalAgencyAgencyOrganizationCustomer
换句话说, 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();
如果“join”只有1级深度,则此方法有效,但无法获取导航属性的导航属性。
然后我尝试了如下连接:
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();
但是,此实例不支持join
。
然后我尝试使用Except
方法如下:
IQueryable 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
端设置一些内容以允许沿定义的导航属性进行简单连接?
我在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();