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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:08:43+00:00 2026-06-01T08:08:43+00:00

I am having trouble trying to represent the below SQL (which returns the results

  • 0

I am having trouble trying to represent the below SQL (which returns the results I want) in LINQ:

select 
    case r.CategoryID
    when 2 then
        case r.PrimaryRecord
            when 1 then r.RecordID
            else (select RecordID from Record where RecordGroupID = r.RecordGroupID and PrimaryRecord = 1)
        end
    else
        r.RecordID
    end
    as RecordID
    , r.FooID
from Record r
where 
    r.FooID = 3

Each row in the Record table has a unique RecordID. Multiple RecordID’s could be associated with a RecordGroupID for CategoryID 2, but only one of them will have the PrimaryRecord field value of 1.

Given the below table of data, my desired output is RecordID = 1, FooID = 3, i.e. the RecordID for the given RecordGroupID that is the PrimaryRecord, but the FooID for the given row that matches my Where clause.

RecordID    RecordGroupID    PrimaryRecord    CategoryID    FooID  
1           1                1                2             1
2           1                0                2             1
3           1                0                2             3

I appreciate the SQL itself probably isn’t the most efficient SQL in the world but it was the best I could come up with.

If anyone could help me create the LINQ statement to represent this query that would be great.

  • 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-01T08:08:44+00:00Added an answer on June 1, 2026 at 8:08 am

    I think you don’t really need the case in the original query. Try something like:

    var matchingRecords = from r in Records
                          where r.FooId = fooId && r.CategoryId == catId && r.RecordGroupId == groupId
                          join r2 in Records on r.RecordGroupId == r2.RecordGroupId && r.CategoryId == r2.CategoryId && r2.PrimaryRecord 
                          select r2;
    

    Edit: added CategoryId in join, assuming RecordGroupId is only unique inside a category.

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

Sidebar

Related Questions

I'm still trying to get the hang of LINQ and am having trouble with
I'm having trouble trying to optimize the following query for sql server 2005. Does
I'm having trouble trying to accomplishing this.. Whatever option I select from a dropdown
I'm having trouble trying to compile a simple fortran program which uses a module
I`m having trouble trying to optimize this query with OVER (PARTITION BY ...) because
I am having trouble trying to get iterators for inner sub-containers. Basically imagine this
I am having trouble trying to connect an Infopath 2007 form to an WCF
I'm having trouble trying to attach a custom tracking participant in workflow foundation 4.0.
I am having trouble trying to move sub nodes or parent nodes up or
I am having trouble trying to make a HashMap accessible to other methods in

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.