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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:30:16+00:00 2026-06-10T06:30:16+00:00

This is the query I need to convert to a LINQ statement for use

  • 0

This is the query I need to convert to a LINQ statement for use inside my ASP.NET MVC 3 project. I’m not all that good with LINQ yet so I’m going to need some help please.

WITH CTE AS (
  SELECT a.GalleryID GalleryID_A, a.GalleryTitle, a.GalleryDate, b.*
        ,ROW_NUMBER() OVER (PARTITION BY a.GalleryID, a.GalleryTitle, a.GalleryDate
                            ORDER BY b.MediaThumb DESC) AS Rn
    FROM Media b
         INNER JOIN Galleries A ON a.GalleryID = b.GalleryID
),

CTE2 AS (
  SELECT a.GalleryID GalleryID_A, a.GalleryTitle, a.GalleryDate, b.*, 1 Rn
   FROM Media b
        LEFT OUTER JOIN Galleries A ON a.GalleryID = b.GalleryID
)

SELECT * FROM CTE where rn = 1

UNION ALL

SELECT * FROM CTE2
  WHERE rn = 1
    AND galleryid is null

ORDER BY MediaID DESC

This was my solution.

var Results = DB.Database.SqlQuery<WhatsNew>
                (@" WITH CTE AS (
                    SELECT a.GalleryTitle, a.GalleryDate, b.*,  
                    ROW_NUMBER() OVER (PARTITION BY  a.GalleryID, a.GalleryTitle, a.GalleryDate 
                    ORDER BY b.MediaThumb DESC) AS Rn
                    FROM Media b
                    INNER JOIN Galleries A
                    ON a.GalleryID = b.GalleryID
                ), 
                CTE2 AS
                (
                    SELECT a.GalleryTitle, a.GalleryDate, b.*,  
                    1 Rn
                    FROM Media b
                    LEFT OUTER JOIN Galleries A
                    ON a.GalleryID = b.GalleryID
                )
                SELECT * FROM CTE where rn = 1
                UNION ALL
                SELECT * FROM CTE2 WHERE rn = 1 and galleryid is null
                ORDER BY MediaID DESC ");
  • 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-10T06:30:17+00:00Added an answer on June 10, 2026 at 6:30 am

    Create your query as a view in sql, say “MyView” and then call that from your MVC project.

    return db.ExecuteStoreQuery<YourEntityType>("SELECT * FROM MyView");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to rewrite this query and I'm not allowed to use a subquery.
i need to convert this query to C# LINQ but i have no idea
I use LINQ, C#, EF4. I have this query expression in Linq. I need
I need to convert this SQL Query to LINQ Query, also I need to
Please i need someone to help convert this query entity(c#) form. select * from
I have to develop a fairly large ASP.NET MVC project very quickly and I
I have a SQL Statement that I am trying to convert to a LINQ
in my current ASP.net MVC 3.0 project i am stuck with a situation. I
I'm having a hard time with this SQL Query. I need to convert this
I am trying to convert this sql into my LINQ query and am having

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.