Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends, i have one task, in this i need to generate list.

C#
foreach (Match m in mc)
           {
               if (m.Groups[1].Value.Trim().Contains(" "))
               {
                   string[] arr = m.Groups[1].Value.Split(' ');
                   foreach (string s in arr)
                       if (!clsList.Contains(s)) clsList.Add(s);
               }
               else
                   if (!clsList.Contains(m.Groups[1].Value)) clsList.Add(m.Groups[1].Value);
           }



in this mc is a match collection, in this i need to convert as list with mc.groups[1].value, then if group value contains space means that must be split and add with list, now only i using foreach loop.

What I have tried:

i tried to convert matchcollection group value to list using linq
Posted
Updated 7-Jun-16 6:37am
Comments
George Jonsson 7-Jun-16 7:04am    
Can you show the text and the regular expression?
I'm not sure you are on the right track here.

try this

C#
mc.OfType<Match>().ToList().ForEach(m => { clsList.AddRange(m.Groups[1].Value.Split(' ')); });
      clsList = clsList.Distinct().ToList();
 
Share this answer
 
Similar to Karthik's solution:
C#
clsList = mc.OfType<Match>().SelectMany(m => m.Groups[1].Value.Split(' ')).Distinct().ToList();
 
Share this answer
 
Comments
Maciej Los 9-Jun-16 15:07pm    
5ed!
Matt T Heffron 9-Jun-16 16:10pm    
Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900