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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:08:34+00:00 2026-05-21T20:08:34+00:00

How do I select the top of the list as long as the items

  • 0

How do I select the top of the list as long as the items at the top of the list have a property with a specific value.

I need the Linq statement to see that there is a break in the sequence and the only return the top two items. The problem is that I don’t know exactly how many items are going to have the correct property value.

I have been working through this problem with LinqPad 4. The code below is a copy and past from LinqPad 4. The results, “q”, should not contain SomeData with an EffectiveDate of 4/5/2011 because the Kind property on hsc2 is “KindTwo”.

I am trying to find the value most resent value of “Kind” and then only take the top records that match that value until I get to a record that doesn’t match that value.

void Main()
{
    var hsc1 = new SomeData {EffectiveDate = new DateTime(2011,4,5), Kind = "KindOne"};
    var hsc2 = new SomeData {EffectiveDate = new DateTime(2011,4,10), Kind = "KindTwo"};
    var hsc3 = new SomeData {EffectiveDate = new DateTime(2011,4,20), Kind = "KindOne"};
    var hsc4 = new SomeData {EffectiveDate = new DateTime(2011,4,25), Kind = "KindOne"};

    var all = new [] {hsc1, hsc2, hsc3, hsc4};

    var lastSomeData = all.OrderByDescending((x) => x.EffectiveDate).First();

    lastSomeData.Dump();

    var q = from h in all 
            where h.Kind == lastSomeData.Kind
            orderby h.EffectiveDate descending 
            select h;

    q.Dump();
}

// Define other methods and classes here
class SomeData
{
    public DateTime EffectiveDate {get;set;}
    public string Kind {get;set;}
}
  • 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-21T20:08:34+00:00Added an answer on May 21, 2026 at 8:08 pm

    This is a fully working Console Application that does what you asked. As I was not the first to propose the use of TakeWhile in this question, please do not mark my answer as the accepted one.

    using System;
    using System.Linq;
    namespace stackoverflow.com_questions_5825629_select_topx_while_x_kind_value
    {
        class Program
        {
            static void Main()
            {
                var hsc1 = new SomeData { EffectiveDate = new DateTime(2011, 4, 5), Kind = "KindOne" };
                var hsc2 = new SomeData { EffectiveDate = new DateTime(2011, 4, 10), Kind = "KindTwo" };
                var hsc3 = new SomeData { EffectiveDate = new DateTime(2011, 4, 20), Kind = "KindOne" };
                var hsc4 = new SomeData { EffectiveDate = new DateTime(2011, 4, 25), Kind = "KindOne" };
    
                var all = new[] { hsc1, hsc2, hsc3, hsc4 };
    
                var lastSomeData = all.OrderByDescending((x) => x.EffectiveDate).First();
    
                var q = (from h in all
                         orderby h.EffectiveDate descending
                         select h).TakeWhile(x => x.Kind == lastSomeData.Kind);
    
                var result = q.ToArray();
    
                foreach (var item in result)
                    Console.WriteLine(item);
                Console.WriteLine("");
                Console.WriteLine("Press any key");
                Console.ReadKey();
            }
    
            // Define other methods and classes here
            class SomeData
            {
                public DateTime EffectiveDate { get; set; }
                public string Kind { get; set; }
                public override string ToString()
                {
                    return string.Format(@"new SomeData {{ EffectiveDate = new DateTime({0}, {1}, {2}), Kind = ""{3}"" }};", EffectiveDate.Year, EffectiveDate.Month, EffectiveDate.Day, Kind);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The below JS is selecting all of the list items, i need to select
I currently have Dropdown at the top of the page and a long list
I have a ListField which is a long vertical list of items you can
I have to select top 5 records and display them in ascending order by
I have a sql like this: SELECT TOP 1 field_name * FROM table_name and
I have the following query: select top 100 eid, cid, id, position, ROW_NUMBER() over(order
I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
I have several comboboxes in a UI each with a long list of similar
I have a list of Channels. I need to use an available one of
I've seen the following used to return a list of numbers SELECT TOP (SELECT

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.