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

The Archive Base Latest Questions

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

I would like to use LINQ to solve the following problem, I have the

  • 0

I would like to use LINQ to solve the following problem, I have the following collection:

List<byte> byteList = new List<byte() { 0x01, 0x00, 0x01, 0x02, 0x01, 0x00, 0x3, 0x4, 0x02 };

The data in this example follows the following pattern:

byteList[0] = address (1, 2, 3, … n)

byteList[1] = old state, which is basically representative of an enum

byteList[2] = new state, same as above

I am interfacing with an embedded device and this is how I can view changes in inputs.

In order to clean up code and make it easier for a maintenance programmer to follow my logic, I’d like to abstract away some of the nuts and bolts involved and extract each three-byte set of data into an anonymous type to be used within the function to perform some additional processing. I’ve written a quick implementation, but I’m sure it can be greatly simplified. I’m trying to clean up the code, not muddy the waters! There has to be a simpler way to do the following:

List<byte> byteList = new List<byte>()
{
    0x01, 0x09, 0x01, 0x02, 0x08, 0x02, 0x03, 0x07, 0x03
};
var addresses = byteList
    .Where((b, i) => i % 3 == 0)
    .ToList();
var oldValues = byteList
    .Where((b, i) => i % 3 == 1)
    .ToList();
var newValues = byteList
    .Where((b, i) => i % 3 == 2)
    .ToList();

var completeObjects = addresses
    .Select((address, index) => new 
    { 
        Address = address,
        OldValue = oldValues[index],
        NewValue = newValues[index]
    })
    .ToList();
foreach (var anonType in completeObjects)
{
    Console.WriteLine("Address: {0}\nOld Value: {1}\nNew Value: {2}\n",
        anonType.Address, anonType.OldValue, anonType.NewValue);
}
  • 1 1 Answer
  • 1 View
  • 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-31T15:03:30+00:00Added an answer on May 31, 2026 at 3:03 pm

    You can use Enumerable.Range and a little math:

    List<byte> byteList = new List<byte>()
    {
        0x01, 0x09, 0x01, 0x02, 0x08, 0x02, 0x03, 0x07, 0x03
    };
    var completeObjects = Enumerable.Range(0, byteList.Count / 3).Select(index =>
        new
        {
            Address = byteList[index * 3],
            OldValue = byteList[index * 3 + 1],
            NewValue = byteList[index * 3 + 2],
        });
    

    If the number of bytes is not a multiple of 3, the extra one or two bytes will be ignored.

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

Sidebar

Related Questions

I have a List(Of MyType) and I would like to use LINQ to get
I have SQL Server database and would like to use LINQ to Entities and
I am new to using LINQ. I would like to use LINQ to retrieve
I have an array of Integers in Java, I would like use only a
I would like to use the following class across multiple modules without needing log
I would like to use Linq and strongly typed views in the right way.
I'd like to use Linq to XML to output a sorted list each element
I am testing out json.net . I would like to use its linq-to-json syntax
I would like to use Linq to Xml to get a single XElement from
I would like to use Linq to query a bus schedule in my project,

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.