2012-03-07 83 views
1

我正在製作Silverlight WCF RIA Services應用程序。雖然,我卡住嘗試不同的方式來使用數據庫中的多個表。 目前,我試圖加入域服務類中的表並將其返回給服務代理。我開始從位於模板項目: http://simplemvvmtoolkit.codeplex.com/wikipage?title=WCF%20RIA%20ServicesWCF RIA服務,加入域服務中的表

我想加盟像表:

public IQueryable<Invoice> GetInvoices() 
    { 
     return (from i in this.ObjectContext.Invoices 
       join o in this.ObjectContext.otherTable equals condition 
       join s in this.ObjectContext.otherTable equals condition 
       select i); 
    } 

此連接的基礎上正確的給定條件的表。但我實際上需要從兩個i.Invoices表& s.otherTable的項目字段。 任何建議,使此投影在DomainServiceClass.cs中工作?

示例代碼USER「克里斯」建議:

public class Invoice_PM 
    { 
    [Key] 
    public int InvoiceId { get; set; } 

    public string PaymentNum { get; set; } 

    public DateTime? PaymentExpDate { get; set; } 

    public DateTime? InvoiceDateTime { get; set; } 

    [Include, Association("name", "InvoiceID", "InvoiceDateTime")] 
    public IEnumerable<InvoiceSoldToShipTo_PM> SoldToShipTo { get; set; } 
} 

回答

0

你的LINQ查詢只能使用連接到基本篩選發票,因爲你是選擇在最後的變量i。我認爲這個問題的答案後會得到你想要的結果:

Error: The entity or complex type cannot be constructed in a LINQ to Entities query

我建議你創建一個自定義類與所有要加載,並選擇您的LINQ語句的結果屬性進入你的自定義類。

自定義類:

public class CustomInvoice 
{ 
    public int InvoiceID { get; set; } 
    public int InvoiceNumber { get; set; } 
    public int ProductID { get; set; } 
    public string ProductName { get; set; } 
    public int InvoiceID { get; set; } 
    public string CustomerName { get; set; } 
} 

域名服務功能:

public IQueryable<CustomInvoice> GetInvoices() 
{ 
    return (from i in this.ObjectContext.Invoices 
      join p in this.ObjectContext.Product equals condition 
      join c in this.ObjectContext.Customer equals condition 
      select new CustomInvoice 
      { 
       InvoiceID = i.ID, 
       InvoideNumber = i.InvoiceNumber, 
       ProductID = p.ID, 
       ProductName = p.Name, 
       CustomerID = c.ID, 
       CustomerName = c.Name 
      };); 
} 

我還沒有嘗試過測試此代碼,但這個想法應該得到你。

+0

當我嘗試這種做法時,會出現一個返回的構建錯誤。 「不一致的可訪問性:返回類型」我相信這是因爲生成的域服務方法與GetInvoices()方法有關。那麼我怎樣才能應用這些更新,刪除和插入功能? – jammer 2012-03-12 13:22:45

+0

鏈接到編譯錯誤:http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=zh-CN&k=k(CS0050);k(VS.ERRORLIST)&rd=true – jammer 2012-03-12 15:48:25

+0

請勿使用生成的方法返回新類型的集合,將生成的方法設置回原來的樣子,返回類型爲IQueryable ,然後手動創建一個類似GetCustomInvoices的新方法,返回類型爲IQueryable 。我認爲這應該解決您的問題,因爲我的域服務中的自定義方法返回自定義類型。 – Chris 2012-03-12 16:16:25