You are on page 1of 2

signup

login

tour

StackOverflowisaquestionandanswersiteforprofessionalandenthusiastprogrammers.It's100%free.

c#HowtoachieveLINQSUMwithmultiplecolumnswithnogroupbyStackOverflow

2015/8/20

help

stackoverflowcareers

Takethe2minutetour

HowtoachieveLINQSUMwithmultiplecolumnswithnogroupby

Iamgettingthedatacorrectly.ButIwanttogettheTotalRunsieifisinSQLQuery,Icanwriteitas
(SUM(pts.Run1)+SUM(pts.Run2)+SUM(pts.Run3)+SUM(pts.Run4)+Sum(pts.Run6))AstotalRuns HowcanIachievethisinLINQ?
Itriedthis onebutitgivesasyntaxerror.
ThisismyLINQQuery.
varplayerScore=fromptsinOritia_entities.PlayerTeamSeasons
joinpinOritia_entities.Playersonnew{ID=pts.PlayerId}
equalsnew{ID=p.ID}
joincinOritia_entities.Crewsonnew{ID=p.CrewId}
equalsnew{ID=c.ID}
joinfinOritia_entities.Fixturesonnew{ID=pts.FixtureId}
equalsnew{ID=f.Id}
wherec.ID==playerID&&pts.SeasonId==seasonID
selectnewPlayerScore
{
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)
};

Iamusing.Net4.0
c# .net linq sum

askedJul4'12at6:55
VeeKayBee
3,352

13

46

70

Whatexactly doyouwanttoachieve?Thesumofallrunsinallfixturesofaplayerinaseason?Andlet
thatsumbepartofthe PlayerScore record?GertArnoldJul5'12at7:42
IlovehowIdidn'trealisethiswascricketrelateduntilIcamebacktothequestionafter24hours...
RawlingJul5'12at7:45
Yes.Ijustwanttocalculatetetheplayer'sscore") VeeKayBee Jul5'12at9:03
Whydownvote? VeeKayBee Jul6'12at5:35
Duplicate:stackoverflow.com/questions/10239987/GertArnoldJul6'12at19:41

1Answer

IfyoujustwanttototalRunsforeachrow,thenyoucando
{
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,

http://stackoverflow.com/questions/11323893/howtoachievelinqsumwithmultiplecolumnswithnogroupby

1/2

2015/8/20

c#HowtoachieveLINQSUMwithmultiplecolumnswithnogroupbyStackOverflow

Dot=(int)pts.Dot,
NoBall=(int)pts.NoBall,
RunOutBy=(int)pts.RunOutBy,
//Match=(t1.T+"v/s"+f.Team2)
};

IfyouwantthetotalrunsforALLrows,afterthequeryyoucoulddo:
varsum=playerScore.Select(x=>x.TotalRuns).Sum();

orifyoudidn'thaveTotalRunsasafield,justmovetheadditionofeachrowtothelamba:
varsum=playerScore.Select(x=>x.Run1+Run2+Run3...).Sum();

editedJul5'12at6:18

answeredJul4'12at7:10
RyanAmies
2,297

26

IsthereisanythingequivalentofSQLSUM? VeeKayBee Jul5'12at4:48


I'lleditmyanswerRyanAmies Jul5'12at6:14

http://stackoverflow.com/questions/11323893/howtoachievelinqsumwithmultiplecolumnswithnogroupby

2/2

You might also like