0

我正在正确获取数据。但我想获得 TotalRuns 即如果在 SQL Query 中,我可以将其写为 (SUM(pts.Run1)+SUM(pts.Run2)+SUM(pts.Run3)+SUM(pts.Run4)+Sum(pts.Run6)) As totalRuns 如何在 LINQ 中实现这一点?

我试过这个,但它给出了一个语法错误。

这是我的 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)

                                              };

我正在使用.Net 4.0

4

1 回答 1

3

如果您只想为每一行计算总运行次数,那么您可以这样做

{
    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)
};

如果您想要所有行的总运行次数,在查询之后您可以执行以下操作:

var sum = playerScore.Select(x=>x.TotalRuns).Sum();

或者,如果您没有 TotalRuns 作为字段,只需将每行的添加移至lamba:

var sum = playerScore.Select(x=>x.Run1 + Run2 + Run3 ...).Sum();
于 2012-07-04T07:10:33.567 回答