Hello my friends, In this tutorial, we are going to understand Count operator in Linq. Count operator is similar to count() in SQL server which is used to return the number of elements in a collection or the number of elements that have satisfied a given condition.

## Count operator in Linq:

Count operator is used for returning the number of elements in a collection or the number of elements that have satisfied a given condition.

1. int Count<TSource>();
2. int Count<TSource>(Func<TSource, bool> predicate);

The first method return the number of elements in a collection.

The second method returns the number of elements in the collection for the specified condition given as lambda expression.

## Example: using the Count method:

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

class Program
{

static void Main(string[] args)
{

IEnumerable<int> items = new List<int> {10,23, 33,44,32,22 };

int count = items.Count();
Console.WriteLine("Total number of elements {0}", count);

}

}```

## Example: Count elements using the query expression-

The output of this example is similar to the above example.

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

class Program
{

static void Main(string[] args)
{

IEnumerable<int> items = new List<int> {10,23, 33,44,32,22 };

int count = (from c in items select c).Count();
Console.WriteLine("Total number of elements {0}", count);

}

}```

## Example: Using Predicate-

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

class Program
{

static void Main(string[] args)
{

IEnumerable<int> items = new List<int> {10,23, 33,44,32,22 };

int count = items.Count(x => x%2==0);
Console.WriteLine("Total number of even elements {0}", count);

}

}```

## Example: Count the number of employees having a salary greater than 30000-

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

public class Employee
{
public string EmployeeCode { get; set; }
public string EmployeeName { get; set; }
public string Gender { get; set; }
public string Designation { get; set; }
public double Salary { get; set; }
}

class Program
{
public static List<Employee> DataSource()
{
List<Employee> lstEmployee = new List<Employee>();
Employee objEmployee1 = new Employee() { EmployeeCode = "E001", EmployeeName = "Ayush Singh", Gender = "Male", Designation = "Software Engineer", Salary = 30000 };
Employee objEmployee2 = new Employee() { EmployeeCode = "E002", EmployeeName = "Ritwik Nigam", Gender = "Male", Designation = "Software Engineer", Salary = 30000 };
Employee objEmployee3 = new Employee() { EmployeeCode = "E003", EmployeeName = "Nikhil Bakshi", Gender = "Male", Designation = "Team Lead", Salary = 60000 };
Employee objEmployee4 = new Employee() { EmployeeCode = "E004", EmployeeName = "Nidhi Verma", Gender = "Female", Designation = "HR", Salary = 40000 };
Employee objEmployee5 = new Employee() { EmployeeCode = "E005", EmployeeName = "Ayushman Lakhotia", Gender = "Male", Designation = "Project Manager", Salary = 80000 };
Employee objEmployee6 = new Employee() { EmployeeCode = "E006", EmployeeName = "Neha Sharma", Gender = "Female", Designation = "Graphic Designer", Salary = 25000 };
return lstEmployee;
}
static void Main(string[] args)
{

int count = DataSource().Count(x => x.Salary>30000);
//the below queries produces the same output
//int count1=(from c in DataSource() where c.Salary>30000).Count();
//int count2 = DataSource().Where(e => e.Salary > 30000).Count();
Console.WriteLine("Total Employees having Salary Greater than 30000 {0}", count);

}

}```

## Conclusion:

I hope you would love this post. Please feel free to comment for any technical help. Your feedback and suggestion are always welcome to me.

Thank You.

### Explore post tags

• I have been examinating out some of your stories and i can state clever stuff. I will definitely bookmark your website.

• Hello to every body, it’s my first visit of this website; this web site carries remarkable and really fine material in support of readers.|

• Hey there this is kinda of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding knowledge so I wanted to get advice from someone with experience. Any help would be enormously appreciated!|

• Wonderful blog you have here but I was curious if you knew of any user discussion forums that cover the same topics talked about here? I’d really love to be a part of community where I can get comments from other knowledgeable people that share the same interest. If you have any suggestions, please let me know. Many thanks!

• canadian pharmacy online arthritis Viagra Soft Tabs

• Just want to say your article is as astonishing. The clarity to your submit is just great and that i could think you are a professional in this subject. Well along with your permission let me to seize your feed to stay up to date with coming near near post. Thanks 1,000,000 and please carry on the enjoyable work.

• Thanks for sharing, this is a fantastic article post.Much thanks again. Want more.

• Thank you ever so for you article. Cool.

• Great blog.Really looking forward to read more. Great.

• Fantastic article.Thanks Again. Really Great.

• Appreciate you sharing, great post.Really looking forward to read more. Fantastic.

• wow, awesome article.Really looking forward to read more. Will read on…

• Thanks a lot for the post. Will read on…

• Thank you, I have recently been looking for information about this subject for ages and yours is the greatest I have discovered till now. But, what about the bottom line? Are you sure about the source?

• Enjoyed every bit of your blog article.Much thanks again. Great.

• Very neat post.Really thank you! Want more.

• I appreciate you sharing this blog article.Really thank you! Want more.

• Looking forward to reading more. Great blog article.Really thank you! Awesome.

• Say, you got a nice article post.Really thank you! Much obliged.

• I cannot thank you enough for the blog article.Much thanks again. Will read on…

• I really like and appreciate your blog article.Much thanks again. Want more.

• Looking forward to reading more. Great article.Much thanks again. Will read on…

• Do you mind if I quote a few of your articles as long as I provide credit and sources back to your blog? My website is in the exact same area of interest as yours and my users would truly benefit from a lot of the information you present here. Please let me know if this okay with you. Appreciate it!|

• I think this is a real great article post. Really Great.

• I really liked your blog.Really looking forward to read more. Want more.

• very nice publish, i certainly love this web site, keep on it

• Very neat blog.Really thank you! Want more.

• Thank you ever so for you article post.Thanks Again. Fantastic.

• Really informative post.Really looking forward to read more. Fantastic.

• Thanks again for the blog post.Much thanks again. Really Great.

• I do enjoy the manner in which you have framed this specific difficulty plus it does present me personally some fodder for consideration. However, coming from what I have experienced, I basically hope when other reviews stack on that folks stay on point and not embark upon a soap box involving the news of the day. Still, thank you for this exceptional point and though I do not necessarily concur with this in totality, I regard the point of view.

• This is one awesome blog post.Much thanks again. Great.

• Thanks for your publication on this web site. From my own experience, there are times when softening right up a photograph might provide the photography with a bit of an inventive flare. Many times however, this soft blur isn’t precisely what you had in mind and can sometimes spoil an otherwise good image, especially if you consider enlarging that.

• I loved as much as you’ll receive carried out right here. The sketch is attractive, your authored subject matter stylish. nonetheless, you command get got an nervousness over that you wish be delivering the following. unwell unquestionably come further formerly again as exactly the same nearly a lot often inside case you shield this hike.|

• Sweet blog! I found it while browsing on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Many thanks

• I cannot thank you enough for the post.Thanks Again.

• Thanks so much for the article.Thanks Again. Fantastic.

• Very informative blog post.Really looking forward to read more. Will read on…

• Thanks a lot for the blog post.Really looking forward to read more. Want more.

• Say, you got a nice post. Great.

• I value the article post.Much thanks again.

• If you wish for to obtain much from this post then you have to apply such strategies to your won weblog.|

• I value the blog.Really looking forward to read more. Cool.

• Thanks again for the post.Thanks Again.

• Superb blog you have here but I was wanting to know if you knew of any message boards that cover the same topics talked about in this article? I’d really like to be a part of group where I can get responses from other knowledgeable people that share the same interest. If you have any suggestions, please let me know. Thank you!|

• Thanks so much for the article post.Really looking forward to read more.

• A big thank you for your article post.Really looking forward to read more. Really Cool.

• An intriguing discussion is worth comment. I do believe that you should publish more on this subject matter, it may not be a taboo subject but usually people do not speak about such topics. To the next! Kind regards!!|

• Thank you for your blog.Thanks Again. Want more.

• It’s a shame you don’t have a donate button! I’d most certainly donate to this fantastic blog! I suppose for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to new updates and will talk about this website with my Facebook group. Talk soon!

• Say, you got a nice blog article.Really thank you! Want more.

• Thank you for your article post.Much thanks again. Really Great.

• I think this is a real great blog article.Really thank you! Awesome.

• Wow, marvelous blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your site is magnificent, as well as the content!

• I truly appreciate this blog. Really Great.

• 762364 359753Most suitable boyfriend speeches, or else toasts. are almost always transported eventually through the entire wedding party and are nonetheless required to be extremely intriguing, amusing and even enlightening together. greatest mans speech 914113

• Hey, thanks for the blog article.Really thank you! Much obliged.

• A lot of of whatever you claim happens to be supprisingly appropriate and that makes me wonder the reason why I had not looked at this in this light before. Your piece really did switch the light on for me as far as this particular issue goes. However at this time there is actually one factor I am not too comfy with so while I attempt to reconcile that with the main theme of your issue, allow me observe just what the rest of the visitors have to point out.Well done.

• Thanks so much for the blog article.Really thank you! Really Great.

• Fantastic blog post.Really looking forward to read more. Great.

• Awesome site you have here but I was curious if you knew of any user discussion forums that cover the same topics talked about here? I’d really love to be a part of online community where I can get comments from other experienced people that share the same interest. If you have any recommendations, please let me know. Many thanks!|

• Fantastic article.Thanks Again. Keep writing.