Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在没有分组的情况下实现多列LINQ求和_C#_.net_Linq_Sum - Fatal编程技术网

C# 如何在没有分组的情况下实现多列LINQ求和

C# 如何在没有分组的情况下实现多列LINQ求和,c#,.net,linq,sum,C#,.net,Linq,Sum,我得到的数据是正确的。但我想得到总运行次数,即如果在SQL查询中,我可以将其编写为 (SUM(pts.Run1)+SUM(pts.Run2)+SUM(pts.Run3)+SUM(pts.Run4)+SUM(pts.Run6))作为总运行次数 如何在LINQ中实现这一点 我尝试了一个,但它给出了一个语法错误 这是我的LINQ查询 var playerScore = from pts in Oritia_entities.PlayerTeamSeasons j

我得到的数据是正确的。但我想得到总运行次数,即如果在SQL查询中,我可以将其编写为
(SUM(pts.Run1)+SUM(pts.Run2)+SUM(pts.Run3)+SUM(pts.Run4)+SUM(pts.Run6))作为总运行次数
如何在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

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

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

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

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

是否有任何与SQL SUM等价的东西?是否有任何与SQL SUM等价的东西?您到底想要实现什么?一名球员在一个赛季的所有赛程中的所有得分总和?让这个总数成为球员核心记录的一部分吧?我喜欢我在24小时后回到问题上来之前没有意识到这与板球有关……是的。我只想计算一下球员的得分)重复:@GertArnold我在问之前搜索了很多,无论如何谢谢你的链接。你到底想达到什么目的?一名球员在一个赛季的所有赛程中的所有得分总和?让这个总数成为球员核心记录的一部分吧?我喜欢我在24小时后回到问题上来之前没有意识到这与板球有关……是的。我只想计算玩家的分数“)重复:@GertArnold我在问之前搜索了很多,不管怎样,谢谢你的链接。我会试试看:)