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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:41:39+00:00 2026-05-19T03:41:39+00:00

I have a query that works perfectly in SQL, but I’m having the damnedest

  • 0

I have a query that works perfectly in SQL, but I’m having the damnedest time converting it to linq. The table (Table1 below) holds status changes for multiple record types. The join requires two fields to be set to create the valid join: A SubmissionId (pk of the table the status pertains to), and a SubmissionTypeId (determines what table the status pertains to).

CREATE TABLE ##Table1 (Id int, Status varchar(50), SubmissionId int, SubmissionTypeId int)
insert into ##Table1(Id, Status, SubmissionId, SubmissionTypeId)
select 1 ,'Status1' ,1 , 1    
union select 2,'Status2',1, 2

CREATE TABLE ##Table2 (ID int, Value varchar(50))
insert into ##Table2 (ID, Value)
select 1, 'Value1Table2'

CREATE TABLE ##Table3 (ID int, Value varchar(50))
insert into ##Table3 (ID, Value)
select 1, 'Value1Table3'

select ds.* from ##Table1 ds
left join ##Table2 di
on ds.SubmissionId = di.Id and ds.SubmissionTypeId = 2
left join ##Table2 dr
on ds.SubmissionId = dr.Id and ds.SubmissionTypeId = 1
where SubmissionTypeId in (1,2)

I’ve tried a couple of iterations using the into x from y in x.DefaultIfEmpty() and I can’t set the where clause in the right location. I need to start the query with Table1 since that is where the values are coming from.

As a work around I split the query into two parts and just added the status data sequentially to a list, but it seems there must be a better way.

Thank you.

  • 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-19T03:41:39+00:00Added an answer on May 19, 2026 at 3:41 am

    I think a direct translation of your SQL would look like this:

    var q = from ds in table1
            where ds.SubmissionTypeId == 1 || ds.SubmissionTypeId == 2
            from di in table2
            from dr in table2
            where (ds.SubmissionTypeId == 2 && ds.SubmissionId == di.Id)
               || (ds.SubmissionTypeId == 1 && ds.SubmissionId == dr.Id)
            select ds;
    

    However, it seems unlikely that this is what you want. If I may speculate about what your intended logic is, I think you want something more like this:

    var q = from ds in table1
            where (ds.SubmissionTypeId == 2 && table2.Any(di => ds.SubmissionId == di.Id))
               || (ds.SubmissionTypeId == 1 && table3.Any(dr => ds.SubmissionId == dr.Id))
            select ds;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a SQL query that works just fine, but am having a
So I have this query that works perfectly: SELECT users.*, GROUP_CONCAT(categories.category_name) AS categories FROM
I have a query that works on Postgresql 7.4 but not on Postgresql 8.3
I have this linq query that works well (although it may be written better,
I have a SQL query that I'm trying to debug. It works fine for
I am using in C# MYsql .I have query that works if I run
I have this query that works fine. Its deletes records that are old based
I have the following query that works well. SELECT DISTINCT city,region1,region2 from static_geo_world where
I have SQL table that has a varchar(8) column that occasionally has binary data
Hi I have a question about SUM in sql, I have a query that

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.