c# - How to Achieve LINQ SUM With Multiple Columns With No Group by - Stack Overflow

download c# - How to Achieve LINQ SUM With Multiple Columns With No Group by - Stack Overflow

of 2

description

Linq

Transcript of c# - How to Achieve LINQ SUM With Multiple Columns With No Group by - Stack Overflow

  • 2015/8/20 c#HowtoachieveLINQSUMwithmultiplecolumnswithnogroupbyStackOverflow

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

    signup login tour help stackoverflowcareers

    Takethe2minutetour StackOverflowisaquestionandanswersiteforprofessionalandenthusiastprogrammers.It's100%free.

    HowtoachieveLINQSUMwithmultiplecolumnswithnogroupby

    Iamgettingthedatacorrectly.ButIwanttogettheTotalRunsieifisinSQLQuery,IcanwriteitasHowcanIachievethisinLINQ?(SUM(pts.Run1)+SUM(pts.Run2)+SUM(pts.Run3)+SUM(pts.Run4)+Sum(pts.Run6))AstotalRuns

    Itried onebutitgivesasyntaxerror.this

    ThisismyLINQQuery.

    varplayerScore=fromptsinOritia_entities.PlayerTeamSeasonsjoinpinOritia_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==seasonIDselectnewPlayerScore{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:55VeeKayBee3,352 13 46 70

    What doyouwanttoachieve?Thesumofallrunsinallfixturesofaplayerinaseason?Andletthatsumbepartofthe record?

    exactlyPlayerScore GertArnold Jul5'12at7:42

    IlovehowIdidn'trealisethiswascricketrelateduntilIcamebacktothequestionafter24hours...Rawling Jul5'12at7:45

    Yes.Ijustwanttocalculatetetheplayer'sscore") VeeKayBee Jul5'12at9:03

    Whydownvote? VeeKayBee Jul6'12at5:35

    Duplicate:stackoverflow.com/questions/10239987/ GertArnold Jul6'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,

  • 2015/8/20 c#HowtoachieveLINQSUMwithmultiplecolumnswithnogroupbyStackOverflow

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

    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:10RyanAmies2,297 9 26

    IsthereisanythingequivalentofSQLSUM? VeeKayBee Jul5'12at4:48

    I'lleditmyanswer RyanAmies Jul5'12at6:14