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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:34:48+00:00 2026-05-16T00:34:48+00:00

I’ve started using Linq-to-entities with .NET 3.5, and I’ve come across a scenario that

  • 0

I’ve started using Linq-to-entities with .NET 3.5, and I’ve come across a scenario that I can do real easy in SQL, but I’m having great difficulty with linq.
I have a table in a SQL database with three fields, for purposes of this example I’ll call them foo, bar, and foo_index and populate them like so:

Blockquote

foo | bar | foo_index

555 101 1

555 101 2

555 101 3

555 101 4

555 101 5

To get what bar is at the max foo_index where foo=555 the SQL is


SELECT bar, max(foo_index) as max_foo_index
FROM T
WHERE foo=555
GROUP BY bar

I’ve written some linq-to-entities code in C# that works, but I have a feeling there is a much more elegent solution out there, and that I’m missing some concept that would make the following much easier. Also, is there any way other then a foreach to get the data out of a var?


db_entity db = new db_entity();

var q =
     from table in db.T
     where table.FOO == 555
     select new { table.BAR, table.FOO_INDEX };

var q2 = 
     from t2 in q
     where t2.FOO_INDEX == table.Max(r => r.FOO_INDEX)
     select new { t2.BAR };

int result = 0;

foreach (var record in q2}
{
    result = record.BAR.Value;
}

Any help or guidance would be appreciated, Thanks!

  • 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-16T00:34:49+00:00Added an answer on May 16, 2026 at 12:34 am

    I think this should work:

    var query = from item in db.T
                where item.FOO == 555
                group item by item.BAR into g
                select new { Bar = g.Key, Max = g.Max(x => x.FOO_INDEX) };
    

    So basically you’re taking the same approach as in the SQL: group the data, then take the key for the group and the maximum FOO_INDEX within the group.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.