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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:41:58+00:00 2026-05-25T23:41:58+00:00

I have a Message object which wraps a message format I do not have

  • 0

I have a Message object which wraps a message format I do not have control over. The format is a simple list of Key/Value pairs. I want to extract a list of Users from a given Message. For example given the following message…

1. 200->....
2. 300->....
3. ....
4. 405->.... 
5. 001->first_user_name
6. 002->first_user_phone
7. 003->first_user_fax
8. 001->second_user_name
9. 001->third_user_name
10. 002->third_user_phone
11. 003->third_user_fax
12. 004->third_user_address
13. .....
14. 001->last_user_name
15. 003->last_user_fax  

I want to extract four Users with the provided properties set. The initial keys 200/300….405 represent fields I don’t need and can skip to get to the User data.

Each users data is in consecutive fields but the number of fields varies depending on how much information is known about a user. The following method does what I’m looking for. It uses an enumeration of possible key types and a method to find the index of the first field with user data.

private List<User> ParseUsers( Message message )
{
    List<User> users = new List<User>( );
    User user = null; String val = String.Empty;

    for( Int32 i = message.IndexOfFirst( Keys.Name ); i < message.Count; i++ ) 
    {
        val = message[ i ].Val;

        switch( message[ i ].Key )
        {
            case Keys.Name:
                user = new User( val );
                users.Add( user ); 
                break;
            case Keys.Phone:
                user.Phone = val;
                break;
            case Keys.Fax:
                user.Fax = val;
                break;
            case Keys.Address:
                user.Address = val;
                break;
            default:
                break;
        }
    }

    return users;
}

I’m wondering if its possible to replace the method with a Linq query. I’m having trouble telling Linq to select a new user and populate its fields with all matching data until you find the start of the next user entry.

Note: Relative key numbers are random (not 1,2,3,4) in the real message format.

  • 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-25T23:41:58+00:00Added an answer on May 25, 2026 at 11:41 pm

    I don’t see the benefit in changing your code to a LINQ query, but it’s definitely possible:

    private List<User> ParseUsers(Message message)
    {
        return Enumerable
            .Range(0, message.Count)
            .Select(i => message[i])
            .SkipWhile(x => x.Key != Keys.Name)
            .GroupAdjacent((g, x) => x.Key != Keys.Name)
            .Select(g => g.ToDictionary(x => x.Key, x => x.Val))
            .Select(d => new User(d[Keys.Name])
            {
                Phone   = d.ContainsKey(Keys.Phone)   ? d[Keys.Phone]   : null,
                Fax     = d.ContainsKey(Keys.Fax)     ? d[Keys.Fax]     : null,
                Address = d.ContainsKey(Keys.Address) ? d[Keys.Address] : null,
            })
            .ToList();
    }
    

    using

    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 a response object which I want to convert to Json, but somehow
I've got an object which is a new MimeMessage called message and I want
I have a web application that uses the CDO Message object to email reports.
I have a web service , i add some extra class which have message
I have a message handler, which consumes from a JMS queue and that sends
I have a message that I want to fadeIn when a form is successfully
I have a simple message box in a WPF application that is launched as
I have implemented a custom list view which looks like the twitter timeline. adapter
I have an object which is added to an objectContext .. after some operation
I have a Message object associated with a User object (user_from and user_to). I

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.