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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:54:10+00:00 2026-05-20T08:54:10+00:00

I have the following SQL query, which I’m struggling to convert to LINQ. Purpose:

  • 0

I have the following SQL query, which I’m struggling to convert to LINQ.

Purpose: Get the top 10 coupons from the table, ordered by the date they expire (i.e. list the ones that are about to expire first) and then randomly choosing one of those for publication.

Notes: Because of the way the database is structured, there maybe duplicate Codes in the Coupon table. Therefore, I am using a GROUP BY to enforce distinction, because I can’t use DISTINCT in the sub select query (which I think is correct). The SQL query works.

SELECT TOP 1
    c1.*
FROM
    Coupon c1
WHERE
    Code IN (
        SELECT TOP 10
            c2.Code
        FROM
            Coupon c2
        WHERE
            c2.Published = 0
        GROUP BY
            c2.Code,
            c2.Expires
        ORDER BY
            c2.Expires
    )
ORDER BY NEWID()

Update:
This is as close as I have got, but in two queries:

var result1 = (from c in Coupons
        where c.Published == false
        orderby c.Expires
        group c by new { c.Code, c.Expires } into coupon
        select coupon.FirstOrDefault()).Take(10);

var result2 = (from c in result1
        orderby Guid.NewGuid()
        select c).Take(1);
  • 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-20T08:54:11+00:00Added an answer on May 20, 2026 at 8:54 am

    Here’s one possible way:

    from c in Coupons
    from cs in 
        ((from c in coupons
            where c.published == false
            select c).Distinct()
            ).Take(10)
    where cs.ID == c.ID
    select c
    

    Keep in mind that LINQ creates a strongly-typed data set, so an IN statement has no general equivalent. I understand trying to keep the SQL tight, but LINQ may not be the best answer for this. If you are using MS SQL Server (not SQL Server Compact) you might want to consider doing this as a Stored Procedure.

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

Sidebar

Related Questions

I have the following which works in SQL Query Analyzer . select oh.* from
I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
I have the following in a SQL Server query which I have to convert
I have following LINQ to SQL query expression from msg in TblUserMessages join user
I have the following Linq to SQL query, in which I'm trying to do
I have the following SQL query: SELECT * FROM table WHERE field_1 <> field_2
I have the following query in SQL which I would like to convert to
I have following somewhat complex sql query which has horrible performance, 'certainly' due to
I have the following query which runs perfectly well on both Oracle and SQL
I have the following SQL Query. SELECT MIN(PLD.available_date) as 'FROM', MAX(PLD.available_date) as 'UNTIL', (

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.