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一側設置一些內容以允許沿定義的導航屬性進行簡單連接?

回答

2

我在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>();