Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Split a collection of strings into some groups

C# Sharp LINQ : Exercise-29 with Solution

Write a program in C# Sharp to split a collection of strings into some groups.

Sample Solution:-

C# Sharp Code:

using System;
using System.Linq;
using System.Collections.Generic;
 
class  LinqExercise29
{
static void Main(string[] args)
    {
       string[] cities =  
            {  
                "ROME","LONDON","NAIROBI","CALIFORNIA",
                "ZURICH","NEW DELHI","AMSTERDAM",
                "ABU DHABI", "PARIS","NEW YORK"  
            };  
  
            Console.Write("\nLINQ : Split a collection of strings into some groups  : "); 
            Console.Write("\n-------------------------------------------------------\n");	
            Console.Write("\nThe cities are : 'ROME','LONDON','NAIROBI','CALIFORNIA','ZURICH','NEW DELHI', \n");
			Console.Write("                   'AMSTERDAM','ABU DHABI','PARIS','NEW YORK' \n");	 
			Console.Write("\nHere is the group of cities : \n\n"); 			

    var citySplit = from i in Enumerable.Range(0, cities.Length)
                                    group cities[i] by i / 3;

    foreach(var city in citySplit)
        cityView(string.Join(";  ", city.ToArray()));
                                         

    Console.ReadLine();
    }
static void cityView(string cityMetro)
{
    Console.WriteLine(cityMetro);
    Console.WriteLine("-- here is a group of cities --\n");
}

}

Sample Output:

LINQ : Split a collection of strings into some groups  :                                                      
-------------------------------------------------------                                                       
    
The cities are : 'ROME','LONDON','NAIROBI','CALIFORNIA','ZURICH','NEW DELHI',                                 
                   'AMSTERDAM','ABU DHABI','PARIS','NEW YORK'                                                 
   
Here is the group of cities :                                                                                 
ROME;  LONDON;  NAIROBI                                                                                       
-- here is a group of cities -- 
                                                                             
CALIFORNIA;  ZURICH;  NEW DELHI                                                                               
-- here is a group of cities --  
                                                                              
AMSTERDAM;  ABU DHABI;  PARIS                                

-- here is a group of cities -- 
                                                                              
NEW YORK                                                                                                   
-- here is a group of cities --

Pictorial Presentation:

C# Sharp LINQ: Split a collection of strings into some groups.

Flowchart:

Flowchart: LINQ : Split a collection of strings into some groups

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to display the list of items in the array according to the length of the string then by name in ascending order.
Next: Write a program in C# Sharp to arrange the distinct elements in the list in ascending order.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.