Return number of item the IEnumerable collection.Returns 0 if there is no item in the collection.Count can be calculated for both LINQ query and Lambda expression.
1) Below is example of count example with LINQ query on a array.
--------------------------------------------------------------
using
System;
using
System.Collections.Generic;
using
System.Data.SqlClient;
using
System.Data.Sql;
using
System.Data;
using
System.Linq;
using
System.Collections;
using
System.Xml;
using
System.Xml.Linq;
using
System.IO;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
int[]
marks = { 10, 20, 30, 40, 50, 60 };
int
sumOfMarks = (from
x in
marks select
x).Count();
Console.WriteLine("The
Count is :"
+ sumOfMarks.ToString());
Console.ReadKey();
}
}
}
//The
Count is :6
-------------------------------------2) Below is example of count example with LINQ query on a collcetion.
--------------------------------------------------------
using System;
using
System.Collections.Generic;
using
System.Data.SqlClient;
using
System.Data.Sql;
using
System.Data;
using
System.Linq;
using
System.Collections;
using
System.Xml;
using
System.Xml.Linq;
using
System.IO;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
IList<student>
myList = new
List<student>()
{
new
student
{ name = "John1",
roll = 121, age = 5 },
new
student
{ name = "John2",
roll = 122, age = 6 },
new
student
{ name = "John3",
roll = 123, age = 7 },
new
student
{ name = "John4",
roll = 124, age = 8 },
new
student
{ name = "John5",
roll = 125, age = 9 },
new
student
{ name = "John6",
roll = 126, age = 10 },
new
student
{ name = "John7",
roll = 127, age = 11 },
new
student
{ name = "John8",
roll = 128, age = 17 },
new
student
{ name = "John9",
roll = 129, age = 18 },
new
student
{ name = "John10",
roll = 1210, age = 19 },
new
student
{ name = "John11",
roll = 1211, age = 20 }
};
var
sumOfAge = myList.Count();
Console.WriteLine("The
Age is :"
+ sumOfAge.ToString());
Console.ReadKey();
}
}
}
public
class
student
{
public
string
name { get;
set;
}
public
int
roll { get;
set;
}
public
int
age { get;
set;
}
};
//The
Age is :11
--------------------------------------------------------------
3)Below is example of count with group by example.
Output :
Name : 5|Count :4
Name : 6|Count :1
Name : 7|Count :1
Name : 9|Count :1
Name : 10|Count :1
Name : 17|Count :1
Name : 19|Count :1
Name : 20|Count :1
3)Below is example of count with group by example.
using
System;
using
System.Collections.Generic;
using
System.Data.SqlClient;
using
System.Data.Sql;
using
System.Data;
using
System.Linq;
using
System.Collections;
using
System.Xml;
using
System.Xml.Linq;
using
System.IO;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
var
myList = new
List<student>
{
new
student
{ name = "John1",
roll = 121, age = 5 },
new
student
{ name = "John2",
roll = 122, age = 6 },
new
student
{ name = "John3",
roll = 123, age = 7 },
new
student
{ name = "John4",
roll = 124, age = 5 },
new
student
{ name = "John5",
roll = 125, age = 9 },
new
student
{ name = "John6",
roll = 126, age = 10 },
new
student
{ name = "John7",
roll = 127, age = 5 },
new
student
{ name = "John8",
roll = 128, age = 17 },
new
student
{ name = "John9",
roll = 129, age = 5 },
new
student
{ name = "John10",
roll = 1210, age = 19 },
new
student
{ name = "John11",
roll = 1211, age = 20 }
};
var
query = from
obj in
myList
group
obj by
obj.age into
countGroup
select
new
{
Name
= countGroup.Key,
Count
= countGroup.Count(),
};
foreach
(var
c in
query)
{
Console.WriteLine("Name
: "
+ c.Name + "|Count
:"
+ c.Count);
}
Console.ReadKey();
}
}
}
public
class
student
{
public
string
name { get;
set;
}
public
int
roll { get;
set;
}
public
int
age { get;
set;
}
};
Output :
Name : 5|Count :4
Name : 6|Count :1
Name : 7|Count :1
Name : 9|Count :1
Name : 10|Count :1
Name : 17|Count :1
Name : 19|Count :1
Name : 20|Count :1
No comments:
Post a Comment