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

  • Home
  • SEARCH
  • 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 676829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:58:20+00:00 2026-05-14T00:58:20+00:00

Let’s say I have an Order table which has a FirstSalesPersonId field and a

  • 0

Let’s say I have an Order table which has a FirstSalesPersonId field and a SecondSalesPersonId field. Both of these are foreign keys that reference the SalesPerson table. For any given order, either one or two salespersons may be credited with the order. In other words, FirstSalesPersonId can never be NULL, but SecondSalesPersonId can be NULL.

When I drop my Order and SalesPerson tables onto the “Linq to SQL Classes” design surface, the class builder spots the two FK relationships from the Order table to the SalesPerson table, and so the generated Order class has a SalesPerson field and a SalesPerson1 field (which I can rename to SalesPerson1 and SalesPerson2 to avoid confusion).

Because I always want to have the salesperson data available whenever I process an order, I am using DataLoadOptions.LoadWith to specify that the two salesperson fields are populated when the order instance is populated, as follows:

dataLoadOptions.LoadWith<Order>(o => o.SalesPerson1);
dataLoadOptions.LoadWith<Order>(o => o.SalesPerson2);

The problem I’m having is that Linq to SQL is using something like the following SQL to load an order:

SELECT ...
FROM Order O
INNER JOIN SalesPerson SP1 ON SP1.salesPersonId = O.firstSalesPersonId
INNER JOIN SalesPerson SP2 ON SP2.salesPersonId = O.secondSalesPersonId

This would make sense if there were always two salesperson records, but because there is sometimes no second salesperson (secondSalesPersonId is NULL), the INNER JOIN causes the query to return no records in that case.

What I effectively want here is to change the second INNER JOIN into a LEFT OUTER JOIN. Is there a way to do that through the UI for the class generator? If not, how else can I achieve this?

(Note that because I’m using the generated classes almost exclusively, I’d rather not have something tacked on the side for this one case if I can avoid it).


Edit: per my comment reply, the SecondSalesPersonId field is nullable (in the DB, and in the generated classes).

  • 1 1 Answer
  • 1 View
  • 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-14T00:58:20+00:00Added an answer on May 14, 2026 at 12:58 am

    The default behaviour actually is a LEFT JOIN, assuming you’ve set up the model correctly.

    Here’s a slightly anonymized example that I just tested on one of my own databases:

    class Program
    {
        static void Main(string[] args)
        {
            using (TestDataContext context = new TestDataContext())
            {
                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<Place>(p => p.Address);
                context.LoadOptions = dlo;
    
                var places = context.Places.Where(p => p.ID >= 100 && p.ID <= 200);
                foreach (var place in places)
                {
                    Console.WriteLine(p.ID, p.AddressID);
                }
            }
        }
    }
    

    This is just a simple test that prints out a list of places and their address IDs. Here is the query text that appears in the profiler:

    SELECT [t0].[ID], [t0].[Name], [t0].[AddressID], ...
    FROM [dbo].[Places] AS [t0]
    LEFT OUTER JOIN (
        SELECT 1 AS [test], [t1].[AddressID],
            [t1].[StreetLine1], [t1].[StreetLine2],
            [t1].[City], [t1].[Region], [t1].[Country], [t1].[PostalCode]
        FROM [dbo].[Addresses] AS [t1]
    ) AS [t2] ON [t2].[AddressID] = [t0].[AddressID]
    WHERE ([t0].[PlaceID] >= @p0) AND ([t0].[PlaceID] <= @p1)
    

    This isn’t exactly a very pretty query (your guess is as good as mine as to what that 1 as [test] is all about), but it’s definitively a LEFT JOIN and doesn’t exhibit the problem you seem to be having. And this is just using the generated classes, I haven’t made any changes.

    Note that I also tested this on a dual relationship (i.e. a single Place having two Address references, one nullable, one not), and I get the exact same results. The first (non-nullable) gets turned into an INNER JOIN, and the second gets turned into a LEFT JOIN.

    It has to be something in your model, like changing the nullability of the second reference. I know you say it’s configured as nullable, but maybe you need to double-check? If it’s definitely nullable then I suggest you post your full schema and DBML so somebody can try to reproduce the behaviour that you’re seeing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have a domain object with the following field: private Map<StatType, Double>
Let's say I have such MYSQL table for logins: id---name---location---login_date ---------------------------------- 1----mike----usa-----21.08.2012 2----tony----uk------22.08.2012 3----tony----uk------23.08.2012
Let's say I have two objects, Master and Slave . Slave has a method
Let's say I have a dataset, which can be neatly classified using weka's J48
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting
Let's say I have a table that lists various toys and the types of
Let's say I have a C++ Visual Studio 2010 solution with 2 projects: one
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:

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.