使用一个linq命令的项目元素的总和

teh*_*ine -1 c# linq

我有一个List ListOfGames,其中VideoGame是:

public class VideoGame{
   public string Title;
   public string Platform;
   public string PublishingCompany;
   public int InStock; //this is how many that we have in stock
   public decimal Price;
   public int GameID; //this is the ID of a specific title. For example: all DOOM games have   id of say 234 regardless of platform.
   //etc.
}
Run Code Online (Sandbox Code Playgroud)

我希望使用简单的LINQ查询来获取某个游戏的库存视频总数.

我试过了:

int stock = ListOfGames.Sum(e=>e.InStock == GameID); 
//GameID is a param that refers to the id of the game.
Run Code Online (Sandbox Code Playgroud)

但是这不会返回正确的值.

我看了这个问题:使用LINQ求和属性,但它没有帮助.关于如何获得特定GameID的所有游戏库存数量总和的任何想法?

Ral*_*ton 5

int stock = ListOfGames.Where(x=>x.Id == GameID).Sum(x=>x.Instock);
Run Code Online (Sandbox Code Playgroud)