English 中文(简体)
将所有+吨数计算在内
原标题:Get all +ve number count in LINQ
  • 时间:2011-11-16 13:04:11
  •  标签:
  • c#
  • .net
  • linq

我使用了以下几件:

int[] a = {1, 2, 3, 0, 5, 0};
int x = a.Select(b => b != 0).Count(); 

我正在拿到6,作为价值,我如何能够获得4,而不是6

请允许我指导我。

最佳回答
int[] a = {1, 2, 3, 0, 5, 0}; 
int x = a.Where(b => b != 0).Count();
问题回答

其他人建议使用<条码>(,后加<条码>,但你可以做更简单的事情:

int x = a.Count(b => b != 0);

http://msdn.microsoft.com/en-us/library/bb535181.aspx>overload of Count 采取先导,基本上只算出与上游相匹配的来源序列的要素。

正如其他人指出的那样,它之所以为<代码>Select做t 工作,是因为只有projects——它就是t filter<>。 http://www.un.org。

// Works but is horrible...
int x = a.Select(b => b != 0).Count(z => z);

页: 1 页: 1

请注意,其中任何一项都不做您的<>/>。 会谈结果见 积极<> > /m> 价值。 为此,请:

int x = a.Count(b => b > 0);

使用<条码>。

 int x = a.Where(b => b != 0).Count();

Select(,每件序列内容都新形式。

2. 改变这一方向

 int[] a = {1, 2, 3, 0, 5, 0}; 
 int x = a.Select(b => b != 0).Count();  

纽约总部

int[] a = {1, 2, 3, 0, 5, 0}; 
int x = a.Where(b => b != 0).Count(); 

不同之处

www.un.org/Depts/DGACM/index_spanish.htm 页: 1

发现物品与这些物品相匹配,只回报这些物品。

-> IEnumerable<A> in, IEnumerable<A> out

<>Select

来源中all物项的收益。 这可能是这些物品本身,但通常是一种某种预测。

- 设计;

你的法典只是将收集工作投向ool,如果其价值不是零,那么,如果是假的,则在档案用户的所在地,你就会真心。

e.g.

int[] a = {1, 2, 3, 0, 5, 0};
int x = a.Where(b => b != 0).Count(); 

您的错误是,{1, 2, 3, 0, 5, 0}.Select(b => b!= 0)只是{true, real,不实,不实},这又是6个项目。 你可以更好地与<代码>保持一致。 如果是,那将给你过滤的序列:{1, 2, 3, 5}。 由此而产生的顺序可适用<条码>。





相关问题
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. ...