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 471463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:59:19+00:00 2026-05-12T23:59:19+00:00

I have the following query which works fine in SSMS. Im using LinqPad (

  • 0

I have the following query which works fine in SSMS. Im using LinqPad (C#) but really puzzling to succeed with the left outer join in LinqToSql:

SELECT DISTINCT 
  A.LocID, 
  V1.PrfValue AS pID, 
  V2.PrfValue AS sID,
  D.DivisionManager, 
  A.IsApproved, 
  A.DateCreated
FROM         
  dbo.Locations AS A 
INNER JOIN
  dbo.Divisions AS D 
  ON    A.DivisionID = D.DivisionID 
LEFT OUTER JOIN
  dbo.ValuesInLocations AS V1 
  ON    A.LocID = V1.LocID 
    AND 
    V1.PrfID IN (SELECT 
            PrfID 
                 FROM 
            dbo.PrfTag 
             WHERE 
            (LevelTypeID = 1)) 
LEFT OUTER JOIN
   dbo.ValuesInLocations AS V2 
   ON   A.LocID = V2.LocID 
    AND 
    V2.PrfID IN (SELECT 
            PrfID 
             FROM 
            dbo.PrfTag 
             WHERE 
            (LevelTypeID = 2))

As you can see, this isn’t the most elegant query to begin work, and I agree that the subquery in both left joins could be improved. However, could you please help me with this translation??

  • 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-12T23:59:19+00:00Added an answer on May 12, 2026 at 11:59 pm

    Following are 2 possible translations of your query. I uses 3 separate queries in the first translation to make it more readable. I hope you find them useful.

    var query1 =
        from prfTag in DataContext.PrfTag
        where prfTag.LevelTypeID = 1
        select PrfID;
    
    var query1 =
        from prfTag in DataContext.PrfTag
        where prfTag.LevelTypeID = 2
        select PrfID;
    
    var query = (
    from A in DataContext.Locations
    join D in DataContext.Divisions
        on A.DivisionID equals D.DivisionID
    join V1 in DataContext.ValueInLocations
        on A.LocID equals V1.LocID
        into VGroup1
    from V1 in VGroup1.DefaultIfEmpty()
    join V2 in DataContext.ValueInLocations
        on A.LocID equals V2.LocID
        into VGroup2
    from V2 in VGroup2.DefaultIfEmpty()
    where (V1 == null || (V1 != null && query1.Contains(V1.PrfID)))
        && (V2 == null || (V2 != null && query2.Contains(V2.PrfID)))
    select new
    {
        A.LocID,
        pID = V1 != null ? V1.PrfValue : "",
        sID = V2 != null ? V2.PrfValue : "",
        D.DivisionManager,
        A.IsApproved,
        A.DateCreated
    }).Distinct();
    

    Here is the second possible translation:

    var query = (
    from A in DataContext.Locations
    join D in DataContext.Divisions
        on A.DivisionID equals D.DivisionID
    join V1 in DataContext.ValueInLocations
        on A.LocID equals V1.LocID
        into VGroup1
    from V1 in VGroup1.DefaultIfEmpty()
    join prfTag1 in DataContext.PrfTag
        on V1.PrfID equals prfTag1.PrfID
    join V2 in DataContext.ValueInLocations
        on A.LocID equals V2.LocID
        into VGroup2
    from V2 in VGroup2.DefaultIfEmpty()
    join prfTag2 in DataContext.PrfTag
        on V2.PrfID equals prfTag2.PrfID
    select new
    {
        A.LocID,
        pID = V1 != null ? V1.PrfValue : "",
        sID = V2 != null ? V2.PrfValue : "",
        D.DivisionManager,
        A.IsApproved,
        A.DateCreated
    }).Distinct();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query which works fine with MySQL but refuses to work
I have the following query which is working fine, but I also want to
I have the following HQL query which works fine, however it returns a list
Got the following simple query which works fine through phpmyadmin but when I add
Hi I have the following script which works fine running on IIS but when
I have the following linq query, which works fine. I'm not sure how i
I have the following loop, which works fine if i<1 but gives me the
I have the following query which works: SELECT ?page ?lat ?long (bif:st_distance(?geo, bif:st_point(42.883, -72.8981)))
I have the following query so far, which works perfectly to rank the books
I have the following which works in SQL Query Analyzer . select oh.* from

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.