Monday, 31 December 2018
Monday, 3 December 2018
Sum (LINQ)
Sum (LINQ)
Enumerable.Sum is extension method from System.Linq namespace. It returns sum of numeric values in collection.
Sum for Numeric Types
Gets sum of values from list of integer numbers.
var numbers = new List<int> { 8, 2, 6, 3 };
int sum = numbers.Sum(); // sum: 19
Gets sum of values from list of decimal numbers.
var numbers = new List<decimal> { 8.1m, 2.2m, 6.1m, 3.3m };
decimal sum = numbers.Sum(); // sum: 19.7
Calling Sum on empty collection returns 0.
var numbers = new List<int>(); // empty list
int sum = numbers.Sum(); // sum: 0
Sum for Nullable Numeric Types
Gets sum of values from list of nullable integers.
var numbers = new List<int?> { 8, 2, null, 3 };
int? sum = numbers.Sum(); // sum: 13
Returns 0 if the collection contains only null values.
var numbers = new List<int?> { null };
int? sum = numbers.Sum(); // sum: 0
Returns 0 if the collection is empty.
var numbers = new List<int?>(); // empty list
int? sum = numbers.Sum(); // sum: 0
Sum with Selector
This example sums lengths of all strings in the list.
var stringList = new List<string> { "88888888", "22", "666666", "333" };
// these two lines do the same
int lengthSum = stringList.Select(x => x.Length).Sum(); // lengthSum: 19
int lengthSum = stringList.Sum(x => x.Length); // lengthSum: 19
Sum with Query Syntax
LINQ query expression to get sum of numeric values in the collection.
var list = new List<int> { 8, 2, 6, 3 };
int sum = (from x in list select x).Sum(); // sum: 19
LINQ query expression to get sum of numbers which match specified predicate.
var list = new List<int> { 8, 2, 6, 3 };
int sum = (from x in list where x > 4 select x).Sum(); // sum: 14
LINQ query expression to get sum of string lengths using selector.
var list = new List<string> { "88888888", "22", "666666", "333" };
int lengthSum = (from x in list select x.Length).Sum(); // lengthSum: 19
Sum with Group By
This example shows how to calculate sum for each group. Lets have players. Each player belongs to a team and have a score. Team total score is sum of score of all players in the team.
var players = new List<Player> {
new Player { Name = "Alex", Team = "A", Score = 10 },
new Player { Name = "Anna", Team = "A", Score = 20 },
new Player { Name = "Luke", Team = "L", Score = 60 },
new Player { Name = "Lucy", Team = "L", Score = 40 },
};
var teamTotalScores =
from player in players
group player by player.Team into playerGroup
select new
{
Team = playerGroup.Key,
TotalScore = playerGroup.Sum(x => x.Score),
};
IQS
- Singleton
- public sealed class Singleton
- {
- Singleton()
- {
- }
- private static readonly object padlock = new object();
- private static Singleton instance = null;
- public static Singleton Instance
- {
- get
- {
- if (instance == null)
- {
- lock (padlock)
- {
- if (instance == null)
- {
- instance = new Singleton();
- }
- }
- }
- return instance;
- }
- }
- }
1)
How to List out Even Numbers sum from a List of
Integers using LINQ
var LstACValues = new List<int> { 1, 7, 2, 5, 10};
var result = (from m in LstACValues
where m % 2 == 0
select
m).ToList().Sum();
2)
What type of information has application object
in asp.net c#
What is an application object?
Application object is used to store the information and access variables from any page in application. Application object is same as session object only the difference is session object is used to maintain the session for particular user. If one user enters in to the application then session id will create for that particular user if he leaves from the application then the session id will deleted. If they again enter in to the application they will get different session id but application object is same for all users once application object is created that application object is used throughout the application regardless of user. The information stored in application object accessed throughout all the pages in application (like database connection information) and we can change the application object in one place those changes automatically reflected in all the pages.
Y create application objects in Global.asax file and access those variables throughout the application.
T know about how to add Global.asax file
<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["UserID"] = "SureshDasari";
}
</script>
A following code in Global.asax file like this
public void Page_Load(object sender, EventArgs e)
| |
{
this.lblText.Text = "UserName: " + Application["UserID"] + "<br>";
}
|
3)
How to call Connection string from application.
Ø
string connectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
4)
When the class library build what files
generated
Ø
Two Files generated
1)
.DLL
2)
.PDB
Subscribe to:
Posts (Atom)