Jason
Jason

Reputation: 371

Basic LINQ to Object codes

I'm learing the LINQ.

I made some codes.

string mySource = @"
#16
100%
Monitor
#19
98%
Guide
#77
0%
Cord
";
var myPattern = @"#(\d+)\r\n(\d+)%\r\n([^\r\n]*)\r\n";
var myCollection = Regex.Matches(mySource, myPattern, RegexOptions.Singleline)
.Cast<Match>().ToList();
MessageBox.Show(string.Join("\n", myCollection));

Looks nice.

But, What I really want to do is like this.

(this kind of data structure)

var myList = new List<string[]>();
var myArray1 = new string[] { "#16", "100%", "Monitor" };
var myArray2 = new string[] { "#19", "98%", "Guide" };
var myArray3 = new string[] { "#77", "0%", "Cord" };
myList.Add(myArray1);
myList.Add(myArray2);
myList.Add(myArray3);

What do I have to do ?

Regards

Upvotes: 0

Views: 43

Answers (1)

Habib
Habib

Reputation: 223257

You can do:

List<string[]> myList = myCollection.Select(r => r.Value
                                                    .Split(new [] { '\r', '\n'},
                                                    StringSplitOptions.RemoveEmptyEntries)
                                           ).ToList();

You will end up with a List<T> with three elements, and each element would consists of array of strings.

Upvotes: 1

Related Questions