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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:47:07+00:00 2026-05-13T20:47:07+00:00

I have the following which works in SQL Query Analyzer . select oh.* from

  • 0

I have the following which works in SQL Query Analyzer.

select oh.*
from order_history oh
join orders o on o.order_id = oh.order_id
where oh.order_id = 20119 and oh.date_inserted = (
    select max(date_inserted) from order_history where order_id = oh.order_id
    group by order_id
)

How would I go about converting to LINQ? From test code, the compiler complained:

Error Operator ‘&&’ cannot be applied to operands of type ‘int’ and ‘System.DateTime’

My LINQ code:

var query = from ch in cdo.order_histories
    join c in cdo.orders on ch.order_id equals c.order_id
    where (ch.order_id.equals(1234)) &&
    (ch.date_inserted == (cdo.order_histories.Max(p => p.date_inserted)))
    select new OrderData() { };

Update: I was not using ‘==’ for comparing.

Item remaining is this from my SQL query:

oh.date_inserted = (
select max(date_inserted) from order_history where order_id = oh.order_id
group by order_id)

How do I do this in LINQ?

  • 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-13T20:47:07+00:00Added an answer on May 13, 2026 at 8:47 pm

    It look like you are missing an equals sign somewhere when filtering on the order_id field. You probably have:

    oh.order_id = 20119 && ...
    

    Whereas you should have:

    oh.order_id == 20119 && ...
    

    Note the equality operator vs. the assignment operator. The result of an assignment operator is the value that was assigned, which is why your error says you can’t compare operands of int and System.DateTime.

    I also assume you have the same problem on the check against the value of date_inserted as well.

    For the second part of your question, you are close in the conversion of the correlated sub query.

    In SQL you have:

    oh.date_inserted = (
    select max(date_inserted) from order_history where order_id = oh.order_id 
    group by order_id)
    

    And in LINQ-to-SQL you have

    ch.date_inserted == (cdo.order_histories.Max(p => p.date_inserted))
    

    You just have to add the filter for the order_histories which takes advantage of closures to capture the order_id value on the ch instance like so:

    ch.date_inserted == (cdo.order_histories.
        Where(ch2 => ch2.order_id == ch.order_id).
        Max(p => p.date_inserted))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.