Reputation: 15650
I am getting the data correctly. But I want to get the TotalRuns ie if is in SQL Query, I can write it as
(SUM(pts.Run1)+SUM(pts.Run2)+SUM(pts.Run3)+SUM(pts.Run4)+Sum(pts.Run6)) As totalRuns
How can I achieve this in LINQ ?
I tried this one but it gives a syntax error.
This is my LINQ Query.
var playerScore = from pts in Oritia_entities.PlayerTeamSeasons
join p in Oritia_entities.Players on new { ID = pts.PlayerId }
equals new { ID = p.ID }
join c in Oritia_entities.Crews on new { ID = p.CrewId }
equals new { ID = c.ID }
join f in Oritia_entities.Fixtures on new { ID = pts.FixtureId }
equals new { ID = f.Id }
where c.ID == playerID && pts.SeasonId == seasonID
select new PlayerScore
{
BallsFaced = (int)pts.BallsFaced,
Run1 = (int)pts.Run1,
Run2 = (int)pts.Run2,
Run3 = (int)pts.Run3,
Run4 = (int)pts.Run4,
Run6 = (int)pts.Run6,
BallsBowled = (int)pts.BallsBowled,
RunsGiven = (int)pts.RunsGiven,
Wickets = (int)pts.Wickets,
Catches = (int)pts.Catches,
Dot = (int)pts.Dot,
NoBall = (int)pts.NoBall,
RunOutBy = (int)pts.RunOutBy,
// Match = (t1.T + " v/s " + f.Team2)
};
I am using .Net 4.0
Upvotes: 0
Views: 7381
Reputation: 4942
If you just want to totalRuns for each row, then you can do
{
BallsFaced = (int)pts.BallsFaced,
Run1 = (int)pts.Run1,
Run2 = (int)pts.Run2,
Run3 = (int)pts.Run3,
Run4 = (int)pts.Run4,
Run6 = (int)pts.Run6,
TotalRuns = (int)pts.Run1 + (int)pts.Run2 + (int)pts.Run3 ...,
BallsBowled = (int)pts.BallsBowled,
RunsGiven = (int)pts.RunsGiven,
Wickets = (int)pts.Wickets,
Catches = (int)pts.Catches,
Dot = (int)pts.Dot,
NoBall = (int)pts.NoBall,
RunOutBy = (int)pts.RunOutBy,
// Match = (t1.T + " v/s " + f.Team2)
};
If you want the total runs for ALL rows, after the query you could do:
var sum = playerScore.Select(x=>x.TotalRuns).Sum();
or if you didn't have TotalRuns as a field, just move the addition of each row to the lamba:
var sum = playerScore.Select(x=>x.Run1 + Run2 + Run3 ...).Sum();
Upvotes: 3