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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:14:52+00:00 2026-05-15T21:14:52+00:00

Example, I have the following interface and classes: public interface IRole { DateTime Since

  • 0

Example, I have the following interface and classes:

public interface IRole {
    DateTime Since {get;}
    DateTime Until {get;}
}

public class Manager : IRole {
    public DateTime Since {get; private set;}
    public DateTime Until {get; private set;}
}
public class Employee : IRole {
    public DateTime Since {get; private set;}
    public DateTime Until {get; private set;}
}
public class Ceo: IRole {
    public DateTime Since {get; private set;}
    public DateTime Until {get; private set;}
}

If a generic list contains the following items:

list[0]=new Manager();
list[1]=new Manager();
list[2]=new Employee();
list[3]=new Manager();
list[4]=new Ceo();
list[5]=new Ceo();

And I shall merge the same types, combine the Since/Until and shrink the items in list, so the output becomes:

newList[0]=new Manager() //(Since is from list[0], Until is from list[1])
newList[1]=new Employee() //(list[2])
newList[2]=new Manager() //(list[3])
newList[3]=new Ceo() //(Since is from list[4], Until is from list[5])

Please make sure you understand the question before answering as I have a history of being ambigous and I don’t want to upset people. So please comment if you feel the “requirement” isn’t clear.

My way is kind of dumb:

for each item in list
    the current item shall always be merged into the previous item
        check if current item has the same type as the previous item
            get last item from newList and merge last item with current item

I was just wondering there must be a better solution.

Updated:

I just realize my “dumb solution” won’t cover cases like more than 2 continuous items with the same type.

Example:

list[0]=new Manager();
list[1]=new Manager();
list[2]=new Employee();
list[3]=new Manager();
list[4]=new Ceo();
list[5]=new Ceo();
list[6]=new Ceo();
  • 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-15T21:14:54+00:00Added an answer on May 15, 2026 at 9:14 pm

    I wrote a blog post about this :-).

    It feels almost like group by except, you don’t want to group elements globally. Instead, you only want to group elements that are adjacent in the input list. The blog post provides some code that allows you to change the meaning of group by in the LINQ query, so you could write just:

    var groups =
      from person in list.WithAdjacentGrouping()
      group person by person.GetType().Name into g
      select new { 
        Type = g.Key,
        Since = new DateTime(g.Select(p => p.Since.Ticks).Min()),
        Until = new DateTime(g.Select(p => p.Until.Ticks).Max())
      }
    

    The call to WithAdjacentGrouping specifes that grouping should only group adjacent elements. Then we can collect adjacent groups of persons by type (using GetType().Name as the key).

    Finally, we return a collection that contains the name of the type (e.g. “Ceo”) and two times – Since and Until that are calculated as minimal/maximal time from the collected group.

    • See Using custom grouping operators in LINQ for the implementation of WithAdjacentGrouping
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 539k
  • Answers 539k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I ended up using SelectMany and Select for this. IConfiguration… May 17, 2026 at 2:20 am
  • Editorial Team
    Editorial Team added an answer first will throw a CastException if invalid. The second will… May 17, 2026 at 2:20 am
  • Editorial Team
    Editorial Team added an answer return theString.replace(/^\/|\/$/g, ''); "Replace all (/.../g) leading slash (^\/) or… May 17, 2026 at 2:20 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Consider the following example. I have an interface MyInterface, and then two abstract classes
I have the following piece of code: class ICookable { public: virtual void CookMe
I have the following interface: internal interface IRelativeTo<T> where T : IObject { T
This problem is best described with an example. I have the following ClientBundle in
HI, I have the following scenario which I will try to explain as best
Is it possible to define an Interface with optional implementation methods? For example I
I have a class called Question that has a property called Type. Based on
I have been very intriuged by design patterns lately, and specifically following correct design
I encountered a problem when I tried to implement the ServiceKnownType example from MSDN.
As we all know, when we derive a class and use polymorphism, someone, somewhere

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.