mithi

This C# Program Divides Sequence into Groups using LINQ. Here it first projects each element of a sequence into a new form and group it.The results are shaped into an enumerable collection of anonymous objects with a property Min and Max.


Here is source code of the C# Program to Divide Sequence into Groups using LINQ. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
  1. /*
  2.  * C# Program to Divide Sequence into Groups using LINQ
  3.  */
  4. using System;
  5. using System.Linq;
  6. using System.IO;
  7. class Program
  8. {
  9.     static void Main(string[] args)
  10.     {
  11.         var seq = Enumerable.Range(100, 100).Select(x => x / 10f);
  12.         var grps = from x in seq.Select((i, j) => new { i, Grp = j / 10 })
  13.                    group x.i by x.Grp into y
  14.                    select new { Min = y.Min(), Max = y.Max() };
  15.         foreach (var grp in grps)
  16.             Console.WriteLine("Min: " + grp.Min + " Max:" + grp.Max);
  17.         Console.ReadLine();
  18.     }
  19. }

Here is the output of the C# Program:
Min : 10  Max : 10.9
Min : 11  Max : 11.9
Min : 12  Max : 12.9
Min : 13  Max : 13.9
Min : 14  Max : 14.9
Min : 15  Max : 15.9
Min : 16  Max : 16.9
Min : 17  Max : 17.9
Min : 18  Max : 18.9
Min : 19  Max : 19.9

1 comments:

  1. Nice and very educational post! Thanks for sharing this informative post with us.

    Website Development Company in USA

    ReplyDelete

 
Top
Mithilesh Joshi