Tuesday 15 February 2011

c# - How to select various-length sections from list using LINQ -


I have a list (this may be a complex type list, but for better idea details I will use the string ).

EDIT: Please, do not provide split method solutions to me, they are completely useless. I have a list of complex types, string is simplified only

There are some repetitive elements that indicate the beginning of the new section

Example:

(In my real problem - every string is a part of the complex type)

  class complex {public string header {get; Set; } ... ... ...}   

Start 1 2 3 4 5 6 7 Start 1 2 3 Start 1 2 3 4 5 6 Start 1 Start 1 2

I want to get all the possible lists using LINQ. I mean

  • 1 2 3 4 5 6 7
  • 1 2 3
  • 1 2 3 4 5 6
  • 1
  • 1 2

    How to use only LINQ?

    use the method, my

      var string = new [ ] {"Start", "1", "2", "3", "4", "5", "6", "7", "start", "1", "2", "3" "Start", "1", "2", "3", "4", "5", "6", "start", "1", "start", "1", "2"}; Strings GroupAgent (v = & gt; v! = "Start"). Where (grp = & gt; grp.Key) // Skip the "Start" item. First (groupItem => console.lightline (string., Group item)));   

    Print:

      1 2 3 4 5 6 7 1 2 3 1 2 3 4 5 6 1 1 2   

    MoreLinq also provides extension method:

      wire. Sample ("Start"). Where (grp = & gt; grp.Any ()) // Leave blank collection as empty item. (Groupings => console.lightline (string .join ("", groupitum));   

    will print the same

No comments:

Post a Comment