![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
GroupBy() Method in C#
The GroupBy() is an extension method that returns a group of elements from the given collection based on some key value.
The following is our array −
int[] arr = { 2, 30, 45, 60, 70 };
Now, we will use GroupBy() to group the elements smaller than 50 −
arr.GroupBy(b => chkSmaller(b));
The above chkSmaller() finds the elements smaller than 50.
Let us see the complete code −
Example
using System; using System.Linq; class Demo { static void Main() { int[] arr = { 2, 30, 45, 60, 70 }; var check = arr.GroupBy(b => chkSmaller(b)); foreach (var val in check) { Console.WriteLine(val.Key); foreach (var res in val) { Console.WriteLine(res); } } } static bool chkSmaller(int a) { return a <= 50; } }
Output
True 2 30 45 False 60 70
Advertisements