Expand and Load Fetching related entities in WCF Data Service

No.of Views4261
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  26 Aug 2010 08:08:13
Tag : WCF , Miscellaneous
In this post, I will show you how to retrieve data from related entities in WCF Data Service.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

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. 

Image Loading

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

1.    Expand
2.    LoadProperty

Using  LoadProperty

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Services.Client;
using ConsoleApplication1.ServiceReference1; 
namespace ConsoleApplication1
{class Program
    {static void Main(string[] args)
        {

            NorthwindEntities context = new NorthwindEntities(new Uri("http://localhost:61091/WcfDataService1.svc/"));try{foreach (Order o in context.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 

Image Loading

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

Output 

Image Loading

Using  Expand

Program.cs

NorthwindEntities context = new NorthwindEntities(new Uri("http://localhost:61091/WcfDataService1.svc/"));
            DataServiceQuery<Order> query =
                context.Orders.Expand("Order_Details,Customer");try{foreach (Order order in query.Take(4))
                {
                    Console.WriteLine("Customer: {0}", order.Customer.ContactName);
                    Console.WriteLine("Order ID: {0}", order.OrderID);foreach (Order_Detail item in order.Order_Details)
                    {
                        Console.WriteLine("\tProduct: {0} - Quantity: {1}",
                            item.ProductID, item.Quantity);
                    }
                }
            }catch (DataServiceQueryException ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.Read();

 So in above code 

Image Loading

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

Image Loading

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

Output 

Image Loading

That's all. enjoy happy coding.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top