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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:28:10+00:00 2026-05-18T01:28:10+00:00

Have the following (non-straightforward) T-SQL query, which i’m trying to convert to LINQ (to

  • 0

Have the following (non-straightforward) T-SQL query, which i’m trying to convert to LINQ (to be used in a L2SQL expression):

declare @IdAddress int = 481887

select * from 
(
    select top 3 p.* 
    from tblProCon p
    inner join vwAddressExpanded a
    on p.IdPrimaryCity = a.IdPrimaryCity
    where a.AddressType = 3
    and p.IsPro = 1
    and a.IdAddress = @IdAddress
    order by AgreeCount desc
) as Pros

union

select * from 
(
    select top 3 p.* 
    from tblProCon p
    inner join vwAddressExpanded a
    on p.IdPrimaryCity = a.IdPrimaryCity
    where a.AddressType = 3
    and p.IsPro = 0
    and a.IdAddress = @IdAddress
    order by AgreeCount desc
) as Cons

order by ispro desc, AgreeCount desc

In a nutshell, i have an @IdAddress – and i’m trying to find the top 3 pro’s and top 3 con’s for that address.

The above query does work as expected. I’m not entirely sure how to convert it to a LINQ query (never done unions before with LINQ). I don’t even know where to start. 🙂

Query-style/Lambda accepted (prefer query-style, for readability).

Also – i have LinqPad installed – but i’m not sure how to “convert T-SQL to Linq” – is there an option for that? Bonus upvote will be awarded for that. 🙂

The above T-SQL query performs well, and this L2SQL query will be executed frequently, so it needs to perform pretty well.

Appreciate the help.

  • 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-18T01:28:11+00:00Added an answer on May 18, 2026 at 1:28 am
    var baseQuery = (from p in db.tblProCon
                    join a in db.vwAddresssExpanded
                    on p.IdPrimaryCity equals a.IdPrimaryCity
                    where a.AddressType == (byte) AddressType.PrimaryCity &&
              a.IdAddress == idAddress
              order by p.AgreeCount descending
                    select p);
    
    var pros = baseQuery.Where(x=> x.IsPro).Take(3);
    var cons = baseQuery.Where(x=> !x.IsPro).Take(3);
    
    var results = pros
                   .Union(cons)
                   .OrderByDescending(x => x.IsPro)
                   .ThenByDescending(x => x.AgreeCount)
                   .ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which I stripped out of any non-essential lines to
I have the following table, from which i have to fetch non unique rows
On a Solaris 5.8 machine, I have the following code: [non-working code] char *buf;
I have the following Solution: SomeProject.Ria (non Silverlight code) SomeProject.Ria.Silverlight (Silverlight light code, namespace
I have the following code: The actual problem is the non-quoted code. I want
I have the following: MODEL.PY LIST = (('Manager', 'Manager'),('Non-Manager', 'Non-Manager'),) class Employee(models.Model): fname =
When we talk about security we have the following requirements: authentication integrity Non-repudiation Isn't
I have set up the OAuth request following the documentation for a Non-Google Service
I tried the following in Clojure, expecting to have the class of a non-lazy
I am trying to persist the location of a window, I have the following

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.