问题
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 ?
回答1:
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.
回答2:
I guess that your question is: can I get to the navigation properties through regular linq statements in stead of the WCF-DS-specific Expand
statement? If so, the answer is "yes, we can".
Of course I can't use your example but if I connect to the Northwind OData service1 I can do:
Order_Details.Expand("Order/Customer").Expand("Product/Order_Details").Take (1)
or
Order_Details.Take(1).Select(o =>
new { o.Order.Customer, o.Product.Order_Details })
Not entirely equivalent (and hardly useful), but OK.
In the URL's you'll see that the second statement is translated to expand
as well.
- http://services.odata.org/V3/Northwind/Northwind.svc/
来源:https://stackoverflow.com/questions/12438481/wcf-data-services-expand-as-linq-statement