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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:14:18+00:00 2026-06-01T21:14:18+00:00

I am trying to reproduce the following SQL query in Linq and need some

  • 0

I am trying to reproduce the following SQL query in Linq and need some help please.

select
   dbo.Documents.DocId, 
   dbo.Documents.ReykerAccountRef, 
   dbo.Documents.ReykerClientId DocClientID,
   CAAs.ClientId CAAClientIDCheck,
   ClientData.FullName ClientFullName, 
   CAAs.IFAId,  
   AdvisorData.FullName AdvisorFullName
from dbo.Documents
left join 
  dbo.CAAs on dbo.Documents.ReykerAccountRef = dbo.CAAs.AccountRef
left join 
  dbo.hmsProfileDatas AS ClientData
on 
  dbo.CAAs.ClientId = ClientData.ReykerClientID
left join 
  dbo.hmsProfileDatas AS AdvisorData
on 
  dbo.CAAs.IFAId = AdvisorData.ReykerClientID

I am trying to link to the same table twice, once for a client fullname and the other for an Advisor fullname.

The basic sql I want to produce in linq is

select table1.*,table2.*,a.Fullname, b.Fullname
from table1
left join
   table2 on table1.t2Id = table2.Id
left join
   table3 AS a 
on 
   table2.t3Id1 = table3.id1
left join
   table3 AS b 
on 
   table2.t3Id2 = table3.id2

So table one is joined to table2 and table2 has 2 foreign keys (t3Id1 and t3Id2) to table3 to different fields (id1 and id2).

This is what I have tried following some guidance, but it’s not returning anything! What’s going wrong?

        var results3 = from doc in DataContext.Documents
                       from caa
                           in DataContext.CAAs
                           .Where(c => c.AccountRef == doc.ReykerAccountRef)
                           .DefaultIfEmpty()
                       from cpd
                           in DataContext.hmsProfileDatas
                           .Where(pdc => pdc.ReykerClientID == caa.ClientId)
                           .DefaultIfEmpty()
                       from apd
                           in DataContext.hmsProfileDatas
                           .Where(pda => pda.ReykerClientID == caa.IFAId)
                           .DefaultIfEmpty()
                       select new DocumentInList()
                                  {
                                      DocId = doc.DocId,
                                      DocTitle = doc.DocTitle,
                                      ReykerDocumentRef = doc.ReykerDocumentRef,
                                      ReykerAccountRef = doc.ReykerAccountRef,
                                      ClientFullName = cpd.FullName,
                                      AdvisorFullName = apd.FullName,
                                      DocTypeId = doc.DocTypeId,
                                      DocTypes = doc.DocTypes,
                                      DocDate = doc.DocDate,
                                      BlobDocName = doc.BlobDocName,
                                      UploadDate = doc.UploadDate,
                                  };
  • 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-06-01T21:14:20+00:00Added an answer on June 1, 2026 at 9:14 pm

    I hope I understood your example correctly. Here’s another simple example, that should give what you need:

        private class User
        {
            public int UserId;
            public string Name;
            public int GroupId;
            public int CollectionId;
        }
    
        public class Group
        {
            public int GroupId;
            public string Name;
        }
    
        public class Collection
        {
            public int CollectionId;
            public string Name;
        }
    
        static void Main()
        {
            var groups = new[] { 
                new Group { GroupId = 1, Name = "Members" },
                new Group { GroupId = 2, Name = "Administrators" } 
            };
            var collections = new[] { 
                new Collection { CollectionId = 1, Name = "Teenagers" },
                new Collection { CollectionId = 2, Name = "Seniors" } 
            };
            var users = new[] { 
                new User { UserId = 1, Name = "Ivan", GroupId = 1, CollectionId = 1 },
                new User { UserId = 2, Name = "Peter", GroupId = 1, CollectionId = 2 },
                new User { UserId = 3, Name = "Stan", GroupId = 2, CollectionId = 1 },
                new User { UserId = 4, Name = "Dan", GroupId = 2, CollectionId = 2 },
                new User { UserId = 5, Name = "Vlad", GroupId = 5, CollectionId = 2 },
                new User { UserId = 6, Name = "Greg", GroupId = 2, CollectionId = 4 },
                new User { UserId = 6, Name = "Arni", GroupId = 3, CollectionId = 3 },
            };
    
            var results = from u in users
                          join g in groups on u.GroupId equals g.GroupId into ug
                          from g in ug.DefaultIfEmpty()
                          join c in collections on u.CollectionId equals c.CollectionId into uc
                          from c in uc.DefaultIfEmpty()
                          select new { 
                              UserName = u.Name, 
                              GroupName = g != null ? g.Name : "<No group>",
                              CollectionName = c != null ? c.Name : "<No collection>"
                          };
        }
    

    It produces two join over one table to get data from other two tables.
    Here’s the output:

    Ivan    Members         Teenagers
    Peter   Members         Seniors
    Stan    Administrators  Teenagers
    Dan     Administrators  Seniors
    Vlad    <No group>      Seniors
    Greg    Administrators  <No collection>
    Arni    <No group>      <No collection>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to reproduce a SQL query using a LINQ to Entities query.
I'm working with a colleague and we're trying to reproduce the following SQL Query
I'm trying to reproduce the heatmap presented on this blog by following their tutorial,
I'm trying to reproduce the layout of some paper forms in a WPF application.
for some reasons I am trying to translate the following RoR view code to
I am trying to reproduce the following but my using rows from the database
Im trying to reproduce the following gradient programmatically. <shape xmlns:android=http://schemas.android.com/apk/res/android> <gradient android:startColor=@color/startcolor android:centerColor=#343434 android:endColor=#00000000
Just getting started with Linq to SQL so forgive the newbie question. I'm trying
I'm trying to reproduce the following IL using Mono.Cecil: call !!0 [mscorlib]System.Threading.Interlocked::CompareExchange<class [System]System.ComponentModel.PropertyChangedEventHandler>(!!0&, !!0,
I'm trying to reproduce the following as a one-liner. if($l < 10) $next =

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.