.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Expand and Load: Fetching related entities in WCF Data Service

Posted By:Dhananjay Kumar       Posted Date: August 24, 2010    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

In this post, I will show you how to retrieve data from related entities in WCF Data Service.
 

Objective

In this post, I will show you how to retrieve data from related entities in WCF Data Service.

If you are new to this topic, please read Introduction to WCF Data service and ODATA before going through below article

Let us say, we want to fetch details from related entities Customers and Order.

WCF1.gif

There are two ways to fetch data from both related entities.

  1. Expand
  1. LoadProperty
Using LoadProperty

Program.cs

using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Data.Services.Client;
using ConsoleApplication1.ServiceReference1; 
namespace ConsoleApplication1
{
classProgram
    {
staticvoid Main(string[] args)
        {

NorthwindEntities context = newNorthwindEntities(newUri("http://localhost:61091/WcfDataService1.svc/")); 
try
            {
foreach (Order o incontext.Orders)
                {
context.LoadProperty(o, "Customer");
Console.WriteLine("Customer : {0}- Order Id: {1}", o.Customer.ContactName, o.OrderID); 

                }
            }
catch (Exception ex)
            {
Console.WriteLine(ex.Message);
            }
Console.Read();
 
        }
    }
}


In above code

WCF2.gif

So in above code load property is being used to load data from related entities.

Output

WCF3.gif

Using Expand

Program.cs

NorthwindEntities context = newNorthwindEntities(newUri("http://localhost:61091/WcfDataService1.svc/"));
DataServiceQuery query =
context.Orders.Expand("Order_Details,Customer");

try
            {
foreach (Orderorderinquery.Take(4))
                {
Console.WriteLine("Customer: {0}", order.Customer.ContactName);
Console.WriteLine("Order ID: {0}", order.OrderID);

foreach (Order_Detail item inorder.Order_Details)
                    {
Console.WriteLine("\tProduct: {0} - Quantity: {1}",
item.ProductID, item.Quantity);
                    }
                }
            }
catch (DataServiceQueryException ex)
            {
Console.WriteLine(ex.Message);
            }

Console.Read();

Console.Read();



So in above code

WCF4.gif

In above code Orders relation is expanded to Order_detail and Customer.

WCF5.gif

In above code retrieving top 4th record and iterating through the records to display.

Output

WCF6.gif

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend