Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WCF RIA服务,连接域服务中的表_C#_Linq_Entity Framework_Silverlight 4.0_Wcf Ria Services - Fatal编程技术网

C# WCF RIA服务,连接域服务中的表

C# WCF RIA服务,连接域服务中的表,c#,linq,entity-framework,silverlight-4.0,wcf-ria-services,C#,Linq,Entity Framework,Silverlight 4.0,Wcf Ria Services,我正在制作Silverlight WCF RIA服务应用程序。尽管如此,我一直在尝试不同的方法来使用数据库中的多个表。 目前,我正在尝试连接域服务类中的表并将其返回给服务代理。我从位于以下位置的模板开始此项目: 我正在尝试连接如下表: public IQueryable<Invoice> GetInvoices() { return (from i in this.ObjectContext.Invoices joi

我正在制作Silverlight WCF 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中工作吗

来自用户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; }
}

您的LINQ查询只使用连接来基本上过滤发票,因为您在最后选择了i变量。我想这篇文章的答案会让你得到你想要的结果:

我建议您创建一个自定义类,其中包含要加载的所有属性,并将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
            }; );  
} 

我还没有试过测试这段代码,但是这个想法应该会让你达到目的。

当我尝试时,会出现一个构建错误。不一致的可访问性:返回类型我相信这是因为生成的域服务方法与GetInvoices方法相关。那么我如何应用这些更新、删除和插入函数呢?链接到编译错误:不要使用生成的方法返回新类型的集合,使用IQueryable返回类型将生成的方法设置回原来的状态,然后使用IQueryable返回类型手动创建一个名为GetCustomInvoices的新方法。我想这应该可以解决你的问题,因为我的域服务中有返回自定义类型的自定义方法。谢谢,现在我收到了这个错误。我已经更新了我的帖子:错误:在实体类型上定义了名为xxx的关联。我在上读到它,我可能应该问你打算如何处理这个自定义对象。您是否只需要此对象来存储自定义查询中的数据,或者确实希望将此类用作域服务对象,并且能够通过此对象对数据进行更改,并且能够通过context.SubmitChanges调用将更改提交到数据库?如果您只想将数据存储在这个对象中,以便在应用程序中显示它,那么您就差不多做到了。如果您希望能够通过此对象将更改更新/保存到数据库中,那么这不是一种方法。