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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:38:32+00:00 2026-05-24T15:38:32+00:00

Say i have a class that contains these items publicly accessible via properties: class

  • 0

Say i have a class that contains these items publicly accessible via properties:

class MyClass
{    
    int switch1; //0 or 1
    int switch2; //0 or 1
    int switch3; //0 or 1
}

This class represents switch states, and each time a switch state changes, i would like to add it to my transition list

I have a large sorted list that contains instances of this class and would like to use a query to capture only the entries in my list where the switch state for any switch changes.

Is this possible using a linq query?

  • 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-24T15:38:32+00:00Added an answer on May 24, 2026 at 3:38 pm

    try this:

    Assuming your class looks like:

    public class State
    {
        public int Id { get; set; }
        public int Switch1 { get; set; }
        public int Switch2 { get; set; }
        public int Switch3 { get; set; }
    
        public override bool Equals(object obj)
        {
            var other = obj as State;
    
            if (other != null)
            {
                return Switch1 == other.Switch1 &&
                       Switch2 == other.Switch2 &&
                       Switch3 == other.Switch3;
            }
    
            return false;
        }
    }
    

    I just added an Equals() to compare flags and my Id field is purely to demonstrate which items changed.

    We can then craft a LINQ query like:

        State previous = null;
        var transitions = list.Where(s =>
                                        {
                                            bool result = !s.Equals(previous);
                                            previous = s;
                                            return result;
                                        })
            .ToList();
    

    Not elegant, but it works, if you had this data set:

        var list = new List<State>
                    {
                        new State { Id = 0, Switch1 = 0, Switch2 = 0, Switch3 = 0 },
                        new State { Id = 1, Switch1 = 0, Switch2 = 0, Switch3 = 0 },
                        new State { Id = 2, Switch1 = 1, Switch2 = 0, Switch3 = 0 },
                        new State { Id = 3, Switch1 = 0, Switch2 = 1, Switch3 = 0 },
                        new State { Id = 4, Switch1 = 0, Switch2 = 1, Switch3 = 0 },
                        new State { Id = 5, Switch1 = 0, Switch2 = 1, Switch3 = 0 },
                        new State { Id = 6, Switch1 = 1, Switch2 = 1, Switch3 = 0 },
                        new State { Id = 7, Switch1 = 0, Switch2 = 0, Switch3 = 1 },
                        new State { Id = 8, Switch1 = 0, Switch2 = 0, Switch3 = 1 },
                        new State { Id = 9, Switch1 = 0, Switch2 = 0, Switch3 = 0 },
                    };
    

    And ran the query, the list would contain your state transitions at items: 0, 2, 3, 6, 7, 9

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

Sidebar

Related Questions

say, I have a partial class that contains a custom property like this public
I have a class that contains a dynamically allocated array, say class A {
Say I have a class that looks like this (get/set omited): class InfoClass{ String
Let's say we have a class that embed another parametric one, that has to
Let's say I have a class that requires configuration, dependency injection etc. public class
Let me explain best with an example. Say you have node class that can
Lets say I have a class A that is fairly simple like this -
Say I have a class which represents a person, a variable within that class
lets say i have a background worker in a class that perform db query
Let's say I have a very simple PrototypeJS class that looks like this: var

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.