Linq to Sql: Retrieve properties from related data (LoadWith)

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)

You have to specify which related-data you want to retrieve from a object so you can access them outside the Linq data-context. You can achieve this by using the LoadWith method of the DataLoadOptions Class. The LoadWith method accepts an lambda expression that specifies which object you want to retrieve.

In the following example I have a employee table that has a relation with the company table. In my code I want to show the employee with the company name (outside the DataContext).


Employee employee;

using (LinqDataContext db = new (LinqDataContext())
{
   DataLoadOptions dlo = new DataLoadOptions(); 
   dlo.LoadWith<Employee>(e => e.Company);
   db.LoadOptions = dlo;

   employee = from item in db.Employees
                      select item).First<Employee>();
}

string companyName = employee.Company.Name;


Because of the DataLoadOptions I can now use the company properties to print the company name outside the DataContext.

Enjoy.

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
Linq to Sql: Retrieve properties from related data (LoadWith) Pieter Brinkman avatarAuthor: Pieter Brinkman ()

Pieter is Technical Marketing Manager for Sitecore Netherlands and owner of Gaatverweg.nl. He has more than ten years experience with software developing in multiple programming languages and with different Content Management Systems. Before joining Sitecore Pieter was a lead developer for multiple Sitecore and .Net projects, he joined Sitecore in 2011 as an Solution Architect in The Netherlands, after two years as an Solution Architect he joined the Technical Marketing department. In the role as Techinical Marketing Manager he is responsible for the Global MVP program and the Sitecore technical branding strategy. You can follow Pieter on twitter: @pieterbrink123 or Google+

Linq to Sql: Retrieve properties from related data (LoadWith) WebsiteLinq to Sql: Retrieve properties from related data (LoadWith) TwitterLinq to Sql: Retrieve properties from related data (LoadWith) Google PlusLinq to Sql: Retrieve properties from related data (LoadWith) Linkedin

Related posts

Asp.Net: keyboard sort items
SQLReporting service: Deployment
Run website on IIS7 (Vista) in classic mode
Daily Tip: Sitecore caching sliding and absolute

0 Responses to Linq to Sql: Retrieve properties from related data (LoadWith)

Comments
Tweets
Pingbacks
  • Click on a tab to select how you'd like to leave your comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>