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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:40:46+00:00 2026-06-09T06:40:46+00:00

I have a list of strings such as this one heading1 00:01:20 randomText 01:23

  • 0

I have a list of strings such as this one

heading1 00:01:20
randomText
01:23
randomText2
01:45
randomText3
02:10
heading2 00:05:20
randomText4
07:25
randomText5
04:35
randomText6
09:12
etc.

What I’d like to do is using Linq to get a list of anonymous types such as

{ Name = "randomText1", Category = "Heading1", Duration = "01:23"}
{ Name = "randomText2", Category = "Heading1", Duration = "01:45"}
...
{ Name = "randomText6", Category = "Heading2", Duration = "09:12"}

Is there any way I could achieve such a result with Linq to Objects? I see how it would be easy to achieve this using for loops, but I am wondering if there would be a cleaner way to do this using Linq, and I can’t seem to find any.

  • 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-06-09T06:40:47+00:00Added an answer on June 9, 2026 at 6:40 am

    This does the trick with the following conditions:

    1) A heading line can be recognized, it starts with a constant value
    (you can change this condition if you want)

    2) For every ‘randomtext’-line, a corresponding duration-line can be found.

    var data = new List<string>() {
        "heading1 00:01:20",
        "randomText       ",
        "01:23            ",
        "randomText2      ",
        "01:45            ",
        "randomText3      ",
        "02:10            ",
        "heading2 00:05:20",
        "randomText4      ",
        "07:25            ",
        "randomText5      ",
        "04:35            ",
        "randomText6      ",
        "09:12            "
    };
    
    const string HEADINGSTART = "heading";                      // TODO: Set correct value
    var temp = Enumerable.Range(0, data.Count - 1)              // Iterate based on index
        .Where(i => data[i].StartsWith(HEADINGSTART))           // Find the headings
        .Select(i => new {                                      // Project to heading + data
            Heading = data[i],
            Data = data.Skip(i + 1).TakeWhile(d => !d.StartsWith(HEADINGSTART)).ToList()
        })
        .SelectMany(d => d.Data.Select(d2 => new {              // Project to single enumerable
            Heading = d.Heading,
            Data = d2
        }))
        .ToList();
    var result = Enumerable.Range(0, temp.Count / 2)            // Again, iterate based on index
        .Select(i => new {                                      // Project to requested object
            Name = temp[i * 2].Data,
            Category = temp[i * 2].Heading,
            Duration = temp[i * 2 + 1].Data
        })
        .ToList();
    
    • 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 strings.. each one looks similar to this: \n\t\BLOCK\,\HEADER-\\r\n\t\t\NAME\,\147430\\r\n\t\t\REVISION\,\0000\\r\n\t\t\DATE\,\11/11/10\\r\n\t\t\TIME\,\10:03:47\\r\n\t\t\PMABAR\,\\\r\n\t\t\COMMENT\,\\\r\n\t\t\PTPNAME\,\0805C\\r\n\t\t\CMPNAME\,\0805C\\r\n\t\BLOCK\,\PRTIDDT-\\r\n\t\t\PMAPP\,1\r\n\t\t\PMADC\,0\r\n\t\t\ComponentQty\,4\r\n\t\BLOCK\,\PRTFORM-\\r\n\t\t\....(more).... What
I have a list containing version strings, such as things: versions_list = [1.1.2, 1.0.0,
I have list of strings like this FirstName-Lastname (separated by a dash -) I
I have a list of strings similar to this list: tags = ('apples', 'apricots',
I have a list of strings like this: my_list = ['Lorem ipsum dolor sit
Say I have a list containing strings that look like this: 00x000s00g00wfds0d dkdf00sdf00sdfg00jk kf00dfjkd0sdl0sd0f0
I have a spinner that is populated with a list of strings such as
I have a List of Strings. EXAMPLE_1, EXAMPLE_2, EXAMPLE_3 ... EXAMPLE_99 What is the
I have sorted list of strings that I move between php and java. to
I have a list of strings that I need to pass to a process

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.