English 中文(简体)
1. 使用1支气筒的物项的元件
原标题:sum of an element of items using one linq command
  • 时间:2011-11-21 15:07:06
  •  标签:
  • c#
  • linq

我有一份名单,显示视频Game:

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.
}

我想获得我们用简明的素福和林研中心为某种游戏所储存的录像气总量。

我尝试:

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

但这不能回去正确的价值。

我研究了这个问题:。 采用LINQ的特性摘要,但不帮助。 关于如何为某个动物群集所有游戏的总和的想法?

最佳回答
int stock = ListOfGames.Where(x=>x.Id == GameID).Sum(x=>x.Instock);
问题回答

我在此建议:

int stock = ListOfGames.Where(e => e.GameID == GameID).Sum(e => e.InStock);

当然,你只是 ne子

var sumOfStock = ListOfGames.Sum(vg => vg.InStock);

InStock以来,您是否希望将财产归到一起?

在您的问询中,你将鱼群(InStock)的游戏次数与动物电离层电离层数作了比较,我确信这并不是你打算做的事。

a. 只获得所有游戏的总和:

int stock = ListOfGames.Sum(e=>e.InStock); 

然而,它还是想通过某种游戏过滤吗? 在此情况下,HoID如何与你的录像节目相对应? 如果你在视频组别上有一个实际的识别点,那么你可以获得这样的数额:

int stock = ListOfGames.Where(e=>e.ID == GameID).Sum(e=>e.InStock); 
int stock = ListOfGames.Where(e => e.GameId == GameID).Sum(e => e.InStock);

如果你想询问所有<代码>VideoGame的合计数,该编码<编码>InStock等于特定GameId 然后审判:

 var query = from game in ListOfGames
                  where game.InStock == GameId
                  select game;
 int sum = query.Sum();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签