简体   繁体   English

WCF RIA服务,域服务中的联接表

[英]WCF RIA Services, Joining Tables in Domain Service

I'm making a Silverlight WCF RIA Services application. 我正在制作Silverlight WCF RIA服务应用程序。 Although, I'm stuck attempting different ways to use multiple Tables in the database. 虽然,我一直坚持尝试不同的方法来使用数据库中的多个表。 Currently, I'm trying to join tables in the Domain Service Class and return it to the service agent. 当前,我正在尝试联接域服务类中的表,并将其返回给服务代理。 I started this project from a template located at: http://simplemvvmtoolkit.codeplex.com/wikipage?title=WCF%20RIA%20Services 我从位于以下位置的模板开始了该项目: http : //simplemvvmtoolkit.codeplex.com/wikipage?title=WCF%20RIA%20Services

I'm trying to join tables like: 我正在尝试加入表,如:

    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); 
    }

This joins the tables correctly based on the given conditions. 这将根据给定条件正确地连接表。 But I actually need to Project Fields from both the i.Invoices Tables & s.otherTable. 但是我实际上需要从i.Invoices Tables和s.otherTable中投影项目字段。 Any suggestions to make this projection work in the DomainServiceClass.cs ? 有什么建议可以使此投影在DomainServiceClass.cs中起作用?

SAMPLE CODE FROM USER "Chris" Suggestion: 用户“ Chris”的示例代码建议:

    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; }
}

Your LINQ query is only using the joins to basically filter the Invoices since you are selecting the i variable at the end. 您的LINQ查询仅使用联接来基本过滤发票,因为您在最后选择了i变量。 I think the answer to this post will get you you the result you want: 我认为这篇文章的答案将为您提供所需的结果:

Error: The entity or complex type cannot be constructed in a LINQ to Entities query 错误:无法在LINQ to Entities查询中构造实体或复杂类型

I suggest you create a custom class with all of the properties that you want to load and select the results of your LINQ statement into your custom class. 建议您使用要加载的所有属性创建一个自定义类,然后将LINQ语句的结果选择到自定义类中。

Custom Class: 自订类别:

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; }
}

Domain Service Function: 域服务功能:

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
            }; );  
} 

I haven't tried tested this code, but the idea should get you there. 我没有尝试测试此代码,但是这个想法应该可以帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM