Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To write a linq query to get selected name count

Tags:

c#

.net

linq

c#-4.0

Here I need to get the total count of name Murugan my output should be "2" . How to write Linq Query for that

Linq:

Person[] names = {new Person { Name = "Murugan", Money = 15000 },
                                 new Person{Name="Vel",Money=17000},
                                 new Person{Name="Murugan",Money=1000},
                                  new Person{Name="Subramani",Money=18000},
                                 new Person{Name="Vel",Money=2500}};
var result = from val in names
         where val.Name == "Murugan" 
         select val;
Console.WriteLine(result);
Console.ReadLine();
like image 952
Vignesh Avatar asked Nov 28 '22 08:11

Vignesh


2 Answers

try this:

var count = names.Count(x=>x.Name=="Murugan");
like image 74
Kamil Budziewski Avatar answered Dec 15 '22 01:12

Kamil Budziewski


You can use this,

var result = (from val in names
                          where val.Name == "Murugan"
                          select val).Count();
like image 29
Sivakumar Avatar answered Dec 15 '22 03:12

Sivakumar