2012-03-07 87 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