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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:31:52+00:00 2026-06-05T05:31:52+00:00

I have an SQL query that I have written using CTE. Now, I am

  • 0

I have an SQL query that I have written using CTE. Now, I am moving the repository to use Entity Framework 5.

I am at a loss as to how to integrate (or rewrite) the CTE-based query using Entity Framework 5.

I am using POCO entities with the EF5 and have a bunch of Map classes. There is no EDMX file etc.

I feel like a total noob right now and would appreciate any help pointing me in the right direction.

The CTE query is as following

WITH CDE AS
(
    SELECT * FROM collaboration.Workspace AS W WHERE W.Id = @WorkspaceId
    UNION ALL
    SELECT W.* FROM collaboration.Workspace AS W INNER JOIN CDE ON W.ParentId = CDE.Id AND W.ParentId <> '00000000-0000-0000-0000-000000000000'
)
SELECT
    W.Id AS Id,
    W.Name AS Name,
    W.Description AS Description,
    MAX(WH.ActionedTimeUtc) AS LastUpdatedTimeUtc,
    WH.ActorId AS LastUpdateUserId
FROM
    collaboration.Workspace AS W
    INNER JOIN
    collaboration.WorkspaceHistory AS WH ON W.Id = WH.WorkspaceId
    INNER JOIN
    (
        SELECT TOP 10
            CDE.Id
        FROM
            CDE
            INNER JOIN
            collaboration.WorkspaceHistory AS WH ON WH.WorkspaceId = CDE.Id
        WHERE
            CDE.Id <> @WorkspaceId
        GROUP BY
            CDE.Id,
            CDE.ParentId,
            WH.ActorId,
            WH.Action
        HAVING
            WH.ActorId = @UserId
            AND
            WH.Action <> 4
        ORDER BY
            COUNT(*) DESC
    ) AS Q ON Q.Id = WH.WorkspaceId
GROUP BY
    W.Id,
    W.Name,
    W.Description,
    WH.ActorId
HAVING
    WH.ActorId = @UserId
  • 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-05T05:31:54+00:00Added an answer on June 5, 2026 at 5:31 am

    You must create stored procedure for your SQL query (or use that query directly) and execute it through dbContext.Database.SqlQuery. You are using code-first approach where you don’t have any other options. In EDMX you could use mapped table valued function but code-first doesn’t have such option yet.

    • 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
I have a big fat query that's written dynamically to integrate some data. Basically
I have a SQL query which is written in string and then executed using
I have a SQL query that I'm trying to debug. It works fine for
I have an SQL query that returns all companies records ignore the ones with
I have a SQL query that I'm trying to write, but I'm not quite
I currently have a SQL query that returns a number of fields. I need
I need to be able to have an SQL query that searches my database
i have a huge sql query that is attached to the rowsource of a
I have a complex sql query that I have in a stored procedure and

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.