首页 > c# > WCF数据服务扩展为Linq声明

WCF数据服务扩展为Linq声明 (WCF Data Services Expand as Linq Statement)

问题

目前我正在尝试使用WCF数据服务。现在我试图通过Include语句向我的Servie查询类似于EF(这是我的服务的数据源)的一些对象,以获得一个objecttree。

我的声明在这一刻看起来像这样:

var query = this.Entities.Veranstaltung.Expand( "VeranstaltungMaterial/Material/Template" ).Expand( "Ort,Dozent" );

var k = query.Where( z => z.DauerTage > 5 && z.TemplateID == null );

是否可以通过LINQ编写Expandstatement,以便自动翻译?

解决方法

Gert的答案是正确的,只是想扩展它(并且无法将其放入注释中):您甚至可以使用LINQ表达扩展的第一个查询:

Order_Details.Take(1).Select(od =>
    new Order_Detail
    {
        Order = new Order
        {
            Customer = od.Order.Customer
        },
        Product = new Product
        {
            Order_Details = od.Product.Order_Details
        },
    });

你可以用m:n关系做类似的技巧。此示例遵循1:n关系,但方向相反:

Customers.Select(c =>
    new Customer
    {
        Orders = c.Orders.Select(o =>
            new Order
            {
                Order_Details = o.Order_Details
            });
    });

这相当于〜/ Customers?$ expand = Orders / Order_Details&$ select = Orders / Order_Details。

问题

at the moment I am trying myself with WCF Data Services. Now I am trying to query my Servie for some objects similar to the EF ( which is my datasource of the Service ) with an Include Statement, to get an objecttree.

My statement looks like this at this moment :

var query = this.Entities.Veranstaltung.Expand( "VeranstaltungMaterial/Material/Template" ).Expand( "Ort,Dozent" );

var k = query.Where( z => z.DauerTage > 5 && z.TemplateID == null );

Is it possible to write the Expandstatement via LINQ so it gets translated automatically ?

解决方法

Gert's answer is correct, just wanted to expand on it (and can't fit it into a comment): You can express even the first query with the Expand just using LINQ:

Order_Details.Take(1).Select(od =>
    new Order_Detail
    {
        Order = new Order
        {
            Customer = od.Order.Customer
        },
        Product = new Product
        {
            Order_Details = od.Product.Order_Details
        },
    });

You can do a similar trick with m:n relationships. This sample follows a 1:n relationship but from the opposite direction:

Customers.Select(c =>
    new Customer
    {
        Orders = c.Orders.Select(o =>
            new Order
            {
                Order_Details = o.Order_Details
            });
    });

This is equivalent to ~/Customers?$expand=Orders/Order_Details&$select=Orders/Order_Details.

相似信息