简体   繁体   English

如何在没有分组依据的情况下实现具有多列的LINQ SUM

[英]How to achieve LINQ SUM with multiple columns with no group by

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 ? 但是我想获得TotalRuns,即如果在SQL查询中,我可以将其写为(SUM(pts.Run1)+SUM(pts.Run2)+SUM(pts.Run3)+SUM(pts.Run4)+Sum(pts.Run6)) As totalRuns如何在LINQ中实现呢?

I tried this one but it gives a syntax error. 我尝试了这个,但是它给出了语法错误。

This is my LINQ Query. 这是我的LINQ查询。

          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 我正在使用.Net 4.0

If you just want to totalRuns for each row, then you can do 如果您只想对每一行进行totalRuns,则可以执行

{
    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: 或者,如果您没有TotalRuns作为字段,则只需将每行的添加项移至lamba:

var sum = playerScore.Select(x=>x.Run1 + Run2 + Run3 ...).Sum();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM