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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:55:58+00:00 2026-05-12T09:55:58+00:00

Appreciate if I can get some help writing a LINQ that will get ALL

  • 0

Appreciate if I can get some help writing a LINQ that will get ALL FIELDS from table A, and those fields for which profile 1 has a value in table AB, show the value, otherwise if profile 1 has no entry in table AB, then show value as null.

Table A

AID    Field
-----------
1      OneField
2      TwoField
3      ThreeField

Table B

BID    Value
-----------
1      OneValue
2      TwoValue
3      ThreeValue

Table AB

ABID   AID   BID  ProfileId
-------------------------
1      1     1    1
2      2     3    1

I’m trying to write a Linq (or sql query) that will show me for profile id 1, all the values from table a, and for those that apply, the value from table b.

e.g.

ProfileID  AID       Field        BID       Value
--------------------------------------------------------
1          1         OneField     1         OneValue
1          2         TwoField     3         ThreeValue
1          3         ThreeField   NULL      NULL

As you can see, the aim is to get ALL FIELDS from table A, and those fields for which profile 1 has an entry in table AB, show the value from table B, otherwise if profile 1 has no entry in table AB, then show as null.

If the solution requires it, I am happy to write that in sql and call it via stored proc.

Thanks

  • 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-12T09:55:58+00:00Added an answer on May 12, 2026 at 9:55 am

    I got a working LINQ solution with a full self contained test. I did not add any asserts at the end but the variable at the end “queryResult” will contain a list with the result you desire. I tested it and verified that it works. Here is the code:

    Edit: In response to your comment Here is example #2

        public class tablea
        {
            public int AID { get; set; }
            public string Field { get; set; }
        }
        public class tableb
        {
            public int BID { get; set; }
            public string Value { get; set; }
        }
        public class tableab
        {
            public int ProfileID { get; set; }
            public int AID { get; set; }
            public string Field { get; set; }
            public int BID { get; set; }
            public string Value { get; set; }
        }
    
        public class result
        {
            public int? profileid { get; set; }
            public int? aid { get; set; }
            public string field { get; set; }
            public string bid { get; set; }
            public string value { get; set; }
        }
    
        [Test]
        public void TestQuery()
        {
            var tablea = new List<tablea>();
            var tableb = new List<tableb>();
            var tableab = new List<tableab>();
    
            tablea.Add(new tablea{AID = 1,Field = "OneField"});
            tablea.Add(new tablea{AID = 2,Field = "TwoField"});
            tablea.Add(new tablea{AID = 3,Field = "ThreeField"});
    
            tableb.Add(new tableb{BID = 1,Value = "OneValue"});
            tableb.Add(new tableb{BID = 2,Value = "TwoValue"});
            tableb.Add(new tableb{BID = 3,Value = "ThreeValue"});
    
            tableab.Add(new tableab{AID = 1,BID=1,ProfileID = 1});
            tableab.Add(new tableab{AID = 2,BID=3,ProfileID = 1});
    
            var profileId = 1;
    
            var q1 = (from a in tablea
                      let bid = (from ab in tableab where ab.ProfileID == profileId && ab.AID == a.AID select ab.BID).FirstOrDefault()
                      let value = (from ab in tableab where ab.ProfileID == profileId && ab.AID == a.AID && ab.BID == bid select ab.Value).FirstOrDefault()
                    select new result
                                {
                                    profileid = profileId,
                                    aid = a.AID,
                                    field = a.Field,
                                    bid = (bid == 0 ? "null" : bid.ToString()),
                                    value = value ?? "null"
                               }).ToList();
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a frustrating problem that I would much appreciate some help with. I
Would really appreciate some help with this. I have an app which consists of
I'm pretty new to Android development and would appreciate some help. All I want
How can I restart(recycle) IIS Application Pool from C# (.net 2)? Appreciate if you
I need help re-factoring this legacy LINQ-SQL code which is generating around 100 update
I could use some help writing a SQL query. I'm trying to display some
I would like to get some help on how to setup paypal payment with
I'm having a little struggle on this one and would appreciate some help. In
Can i get the source code for a WAMP stack installer somewhere? Any help
I would be extremely appreciative if anybody can help me. I am learning C++

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.