Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 526065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:44:14+00:00 2026-05-13T08:44:14+00:00

We have several classes with multiple 1:1 Relationships for quick joins, and while this

  • 0

We have several classes with multiple 1:1 Relationships for quick joins, and while this works fine for anonymous types for tabular display, I’m unsure how to fully populate the type in a single linq query.

We have these properties either because it’s an off 1:1, or we don’t want to query through a child collection to find a “primary” every display, we instead incur the cost by setting these Primary IDs on save.

A stripped down example for the context of this post:

public class Contact
{
  public long Id { get; set; }

  public EntitySet<Address> Addresses { get; set; }
  public EntityRef<Address> PrimaryAddress { get; set; }
  public long? PrimaryAddressId { get; set; }

  public EntitySet<Email> Emails { get; set; }
  public EntityRef<Email> PrimaryEmail { get; set; }
  public long? PrimaryEmailId { get; set; }

  public string FirstName { get; set; }
  public string LastName { get; set; }
}

public class Address
{
  public long Id { get; set; }
  public EntitySet<Contact> Contacts { get; set; }

  public bool IsPrimary { get; set; }
  public string Street1 { get; set; }
  public string Street2 { get; set; }
  public string City { get; set; }
  public string State { get; set; }
  public string Country { get; set; }
}

public class Email
{
  public long Id { get; set; }
  public EntitySet<Contact> Contacts { get; set; }

  public bool IsPrimary { get; set; }
  public string Address { get; set; }
}

The problem is when displaying a list of contacts, the PrimaryAddress and PrimaryEmail have to be lazy loaded. If we do DataLoadOptions it doesn’t give the desired effect either since it’s a 1:1, example:

var DB = new DataContext();
var dlo = new DataLoadOptions();
dlo.LoadWith<Contact>(c => c.PrimaryAddress);
dlo.LoadWith<Contact>(c => c.PrimaryEmail);
DB.LoadOptions = dlo;

var result = from c in DB.Contacts select c;
result.ToList();

The above code results in a INNER JOIN since it treats it like a parent relationship, it doesn’t respect the nullable FK relationship and left join the 1:1 properties. The desired query would be something like:

Select t1.*, t.2*, t3.*
From Contact t1
Left Join Address t2 On t1.PrimayAddressId = t2.Id
Left Join Email On t1.PrimaryEmailId = t3.Id

Is there a way to do this and get a IQueryable with these nullable 1:1 properties populated, or even a List? Due to other constraints, we need the type to be Contact, so anonymous types won’t work. Pretty open to options, anything would be better than lazy loading n*(number of 1:1s)+1 queries for the number of rows we display.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:44:15+00:00Added an answer on May 13, 2026 at 8:44 am

    Update: Finally got around to updating this, the devart guys have fixed the behavior in later versions to work perfectly. There’s no need for DataLoadOptions at all, just using fields off the table works, for example:

    var DB = new DataContext();
    var result = from c in DB.Contacts
                 select new {
                   c.Id
                   c.FirstName,
                   c.LastName,
                   Address = c.PrimaryAddress.Street1 + " " + c.PrimaryAddress.Street2 //...
                   Email = c.PrimaryEmail.Address
                 };
    

    This correctly performs a single left outer join to the related Address and Email tables. Now the fix is specific to the situation here of getting this anonymous type…but they also fixed the DataLoadOptions behavior where we do need it, correctly keyed off the foreign key type now. Hope this update helps others on an older version…I highly recommend upgrading, there are lots of new enhancements in versions since 5.35 (many making life much easier).


    Original:
    What we ended up with was a different approach. This may be specific behavior to the devart: dotConnect for Oracle provider (as of version 5.35.62, if this behavior changes I’ll try and update this question).

    var DB = new DataContext();
    var result = from c in DB.Contacts
                 select new {
                   c.Id
                   c.FirstName,
                   c.LastName,
                   Address = new AddressLite { 
                                   Street1 = c.PrimaryAddress.Street1, 
                                   Street2 = c.PrimaryAddress.Street2, 
                                   City = c.PrimaryAddress.City,
                                   State = go.PrimaryAddress.State,
                                   Country = go.PrimaryAddress.Country },
                   Email = c.PrimaryEmail.Address
                 };
    result.ToList();
    

    This results in a single query. While calling a child object in the select, e.g. c.PrimaryAddress does not cause a join to occur (resulting in a lot of select ... from address where id = n lazy loads, one per row of tabular data we’re displaying), calling a property on it however, e.g. c.PrimaryAddress.Street1 DOES cause a correct left join in the address table in the query query. The linq above works only in linq-to-sql, it would fail with null reference on linq-to-entities, but…in the case we’re dealing with that’s fine.


    The good:

    • Single query, producing left joins to Address and Email
    • Lightweight objects for address and down to just a string for email (they both have some back-reference EntiySet in the real project, making them more expensive than necessary for simple tabular display needs)
    • Fast/clean, the above is a much simpler query than manually joining every child table we were doing, cleaner code.
    • Performance, the creation of the heavier objects was quite a hit, changing from Email to string, Address to AddressLite and (in the full project) Phone to PhoneLite resulted in pages just displaying tabular data going from 300-500ms down to 50-100ms.

    The Bad:

    • Anonymous type, there are cases where we need a strong type, having to create those (even as quick as ReSharper makes this task) adds a lot of clutter.
    • Since we can’t modify and save an anonymous type, or any type we create without a good deal of annotation work, which must be updated if the model changes anything around that. (since those classes aren’t generated)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 247k
  • Answers 247k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer unlike the CSS tables debate, there are clear security implications… May 13, 2026 at 8:44 am
  • Editorial Team
    Editorial Team added an answer There is no way to do this. May 13, 2026 at 8:44 am
  • Editorial Team
    Editorial Team added an answer You can't authenticate a user by looking in the Global… May 13, 2026 at 8:44 am

Related Questions

I am using the singleton pattern in all my PHP class files. Would it
I'm trying to map a collection (of type map) using a foreign key and
I have been bitten by a poorly architected solution. It is not thread safe!
In Visual Studio 2008 using C#, what is the best way to share code
I have a very simple application which consists of an ASP.NET front end site,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.