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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:14:40+00:00 2026-06-16T05:14:40+00:00

I am trying to reproduce a SQL query using a LINQ to Entities query.

  • 0

I am trying to reproduce a SQL query using a LINQ to Entities query. The following SQL works fine, I just don’t see how to do it in LINQ. I have tried for a few hours today but I’m just missing something.

SELECT 
  h.ReqID,
  rs.RoutingSection
FROM ReqHeader h
JOIN ReqRoutings        rr ON rr.ReqRoutingID = (SELECT TOP 1   r1.ReqRoutingID  
                                                FROM ReqRoutings r1
                                                WHERE r1.ReqID = h.ReqID 
                                                ORDER BY r1.ReqRoutingID desc)
JOIN ReqRoutingSections rs ON rs.RoutingSectionID =  rr.RoutingSectionID  

Edit***

I was able to get this working after looking at other examples including the one provided her by Miki. Here is the code that works for me:

First I created a query called route to hold the top record I needed to join to

var route = (from rr in context.ReqRoutings
                     where rr.ReqID == id
                     orderby rr.ID descending
                     select rr).Take(1);

I was then able to join to my requisitions table and the ReqRoutings lookup table

var header = (from h in context.ReqHeaders
              join r in route on h.ID equals r.ReqID
              join rs in context.ReqRoutingSections on r.RoutingSectionID equals rs.ID
              where h.ID == id

              select {ReqID = h.ID,
                      RoutingSection = rs.RoutingSection}
  • 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-16T05:14:42+00:00Added an answer on June 16, 2026 at 5:14 am

    I am using Northwnd sample database
    Customers,Orders,Employees table
    Here I am getting top 1 order group by customer and order’s employeeid
    Please let me know If this is matching with your requirement or not

    var ord = from o in NDC.Orders
              orderby o.OrderID descending
              group o by o.CustomerID into g                     
              select new {CustomerID=g.Key,Order=g.OrderByDescending(s=>s.OrderID).First() };
    
    var res1 = from o in ord
               join emp in NDC.Employees 
               on o.Order.EmployeeID equals emp.EmployeeID into oemp
               select new {Order=o.Order,employee=oemp };
    
    Response.Write(res1.ToList().Count);
    foreach (var order in res1)
    {
        Response.Write(order.Order.CustomerID + "," + 
                         order.Order.OrderID + ","+ 
                         order.Order.EmployeeID+"<br/>");
    }
    

    // Above code is working .I have tried to convert your query to linq and replace your datacontext name with ‘NDC’

    var ord = from rr in NDC.ReqRoutings
              orderby rr.ReqRoutingID descending
              group rr by rr.ReqID into g
              select new 
              { 
                  ReqID = g.Key, 
                  ReqRoutings = g.OrderByDescending(s => s.ReqRoutingID).First() 
              };
    
    var res1 = from o in ord
               join emp in NDC.ReqRoutingSections on o.ReqRoutings.RoutingSectionID 
                 equals emp.RoutingSectionID into oemp
               select new { ReqRoutings = o.ReqRoutings, employee = oemp };
    
    Response.Write(res1.ToList().Count);
    foreach (var order in res1)
    {
        Response.Write(order.ReqRoutings.ReqID + "," + 
                        order.ReqRoutings.ReqRoutingID + "," + 
                        order.ReqRoutings.RoutingSectionID + "<br/>");
    }
    

    Please let know if it is help you or not

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

Sidebar

Related Questions

I have a complex query that I'm trying to reproduce in LINQ to Entities,
I am trying to reproduce the following SQL query in Linq and need some
I'm working with a colleague and we're trying to reproduce the following SQL Query
I am trying to reproduce the following but my using rows from the database
I am trying to reproduce a query using the Nhibernate Criteria API and cannot
I'm trying to reproduce this SQL query I used with ActiveRecord: Post.all(:conditions => [EXTRACT(MONTH
Just getting started with Linq to SQL so forgive the newbie question. I'm trying
I am trying to reproduce a simple jquery slider example that works on this
I'm trying to reproduce the heatmap presented on this blog by following their tutorial,
I am trying to reproduce the same behavior as EntityObject using CTP5 DBContext for

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.