2012-09-05 106 views
1

考慮下面的示例模型(這反映了我的實體框架的實體,不再贅述屬性):WCF數據服務集合裝載

public class Company 
{ 
    public string Name { get; set; } 
    public IList<Department> Departments { get; set; } 
} 

public class Department 
{ 
    public string Name { get; set; } 
    public IList<Employee> Employees { get; set; } 
} 

public class Employee 
{ 
    public string Name { get; set; } 
} 

我從我的Web UI消費WCF數據服務來獲得這些實體。我基本上是一個最佳實踐/更優雅的方式加載每個Company將具有各自的Department,將有各自Employee急切加載。

目前我的代碼看起來是這樣的:

var proxy = new MyWCFDataService(url); 

var companies = proxy.Companies; 

foreach (var company in companies) 
{ 
    proxy.LoadProperty(company, "Departments"); 

    foreach (var dept in company.Departments) 
    { 
     proxy.LoadProperty(dept, "Employees"); 
    } 
} 

如果我必須做到上面的話,我想這樣做的服務工作,所以我可以得到這個意大利麪條惹出來我的UI控制器。我試着用下面這樣做:

[WebGet] 
public IQueryable<Company> GetCompanies() 
{ 
    var companies = this.CurrentDataSource.Companies.Include("Departments"); 

    return companies; 
} 

我得到我Company回罰款,但我又沒有Department收集它只是一個空列表。

任何想法的人?

回答

4
var companies = proxy.Companies.Expand("Departments"); 
+0

已經嘗試過這一點,它工作正常......但...這不會加載每個'Department'的'員工'集合。 –

+0

var companies = proxy.Companies.Expand(「Departments」)。Expand(「Department/Employees」) – Geoff

+0

Yes !!!這完全是我在找的,謝謝! –