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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:03:31+00:00 2026-05-27T07:03:31+00:00

I have a troublesome query to write. I’m currently writing some nasty for loops

  • 0

I have a troublesome query to write. I’m currently writing some nasty for loops to solve it, but I’m curious to know if Linq can do it for me.

I have:

struct TheStruct
{
 public DateTime date {get; set;} //(time portion will always be 12 am)
 public decimal A {get; set;}
 public decimal B {get; set;}
}

and a list that contains these structs. Let’s say it’s ordered this way:

List<TheStruct> orderedList = unorderedList.OrderBy(x => x.date).ToList();

If you put the orderedList struct dates in a set they will always be contiguous with respect to the day.. that is if the latest date in the list was 2011/01/31, and the earliest date in the list was 2011/01/01, then you’d find that the list would contain 31 items, one for each date in January.

Ok, so what I want to do is group the list items such that:

  1. Each item in a group must contain the same Decimal A value and the same Decimal B value
  2. The date values in a group must form a set of contiguous dates, if the date values were in order
  3. If you summed up the sums of items in each group, the total would equal the number of items in the original list (or you could say a struct with a particular date can’t belong to more than one group)

Any Linq masters know how to do this one?

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-27T07:03:31+00:00Added an answer on May 27, 2026 at 7:03 am

    You can group adjacent items in a sequence using the GroupAdjacent Extension Method (see below):

    var result = unorderedList
        .OrderBy(x => x.date)
        .GroupAdjacent((g, x) => x.A == g.Last().A && 
                                 x.B == g.Last().B && 
                                 x.date == g.Last().date.AddDays(1))
        .ToList();
    

    Example:

    (1,1) 2011-01-01   \
    (1,1) 2011-01-02    >  Group 1
    (1,1) 2011-01-03 __/
    (2,1) 2011-01-04   \
    (2,1) 2011-01-05    >  Group 2
    (2,1) 2011-01-06 __/
    (1,1) 2011-01-07   \
    (1,1) 2011-01-08    >  Group 3
    (1,1) 2011-01-09 __/
    (1,1) 2011-02-01   \
    (1,1) 2011-02-02    >  Group 4
    (1,1) 2011-02-03 __/
    

    Extension Method:

    static IEnumerable<IEnumerable<T>> GroupAdjacent<T>(
        this IEnumerable<T> source, Func<IEnumerable<T>, T, bool> adjacent)
    {
        var g = new List<T>();
        foreach (var x in source)
        {
            if (g.Count != 0 && !adjacent(g, x))
            {
                yield return g;
                g = new List<T>();
            }
            g.Add(x);
        }
        yield return g;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some troublesome form in my django app. Problem is that it doesn't
I have a linq query that is passed into NHibernate which Linq2NHibernate will parse
I have a noob question but rather a troublesome one for me. I am
this is my first question on stack overflow but I have some experience in
We have SharePoint 2007, running II6... Currently we have a problem with a troublesome
Question 1: I have some XML-based layouts which work fine on phones. But when
I'm writing a particularly troublesome query. It boils down to this: I obtain a
I have a troublesome MySQL query as follows: SELECT camera_id, ((avg(low_price) + avg(high_price)) /
I've tried Zend PDF and it works pretty well but I have a troublesome
have not tested on windows. but in ubuntu when u disconnect from the network,

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.