Sum() method return the calculated sum of numeric items in the collection .Below example is the example of Sum method of LINQ query.
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)
{
int[]
marks = { 10, 20, 30, 40, 50, 60 };
int
sumOfMarks = (from
x in
marks select
x).Sum();
Console.WriteLine("The
Sum is :"
+ sumOfMarks.ToString());
Console.ReadKey();
}
}
}
//The
Sum is :210
----------------------------------
----------------------------------
Below example is the example of Sum method of Lambda query on a array.
Example
-------------------------------------------
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)
{
int[]
marks = { 10, 20, 30, 40, 50, 60 };
double
sumOfMarks = marks.Sum(a=>a);
Console.WriteLine("The
Sum is :"
+ sumOfMarks.ToString());
Console.ReadKey();
}
}
}
//The
Sum is :210
----------------------------------------------------
Below example is the example of Sum method of LINQ query on a collections.
---------------------------------------------------
Below example is the example of Sum method of LINQ query on a collections.
---------------------------------------------------
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.Sum(a => a.age);
Console.WriteLine("The
Sum is :"
+ sumOfAge.ToString());
Console.ReadKey();
}
}
}
public
class
student
{
public
string
name { get;
set;
}
public
int
roll { get;
set;
}
public
int
age { get;
set;
}
};
--------------------------------------------------------
Below example is the example of Sum method of Lambda query on a collcetions.
---------------------------------------------------
---------------------------------------------------
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.Max(a => a.age);
Console.WriteLine("The
Sum is :"
+ sumOfAge.ToString());
Console.ReadKey();
}
}
}
//The
Maximum is :60
public
class
student
{
public
string
name { get;
set;
}
public
int
roll { get;
set;
}
public
int
age { get;
set;
}
};
No comments:
Post a Comment