2

How can I count elements that are equal to 0 in each array of a list?

I have a list List<byte[]> piks. I would like to count in each byte[] how many elements are with equal to 0.

I tried a few ways:

from c in piksle_lista_tablic[84] 
where (c.Equals(0)) 
select c

or

piksle_lista_tablic[84].Count(n => n == 0)

and I always get the error Expression cannot contain lambda expressions.

For example: piks[1] is an array containing 1156 items, and I would like to know how many specific elements are in that array.


PS: Can i use Linq in watch window?

Rune FS
  • 21,497
  • 7
  • 62
  • 96
deadfish
  • 11,996
  • 12
  • 87
  • 136

4 Answers4

5
var results = from arr in piks
              select arr.Where(b=>b==0).Count()

that code will iterate the list of array and for each array find the elements equalling zero and return an IEnumerable with the counts for each array. I like the where count more than the Count(selector) but that a matter of taste. I doubt there's going to be noticeable difference performancewise

to you ps 1 yes you can use linq while debugging but it's generally a pain because a linq statement is one statement chopping it up in methods can sometimes help while debugging but I dislike writing code for the sake of the debugger.

EDIT As per your comment: No you cannot use Lambda in the watch window. You can use Linq in the watch window but only as method calls and only with named functions

Rune FS
  • 21,497
  • 7
  • 62
  • 96
  • This code and under won't work in debug, i tried.. But thanks, the problem was in my methodology :) – deadfish Oct 27 '11 at 20:29
  • @Cooldown4seconds it will work in debug are you talking about the watch window/immediate window? if that's so then you're correct you cannot use lambda expressions in there – Rune FS Oct 28 '11 at 10:49
4

Try this:

var zero_counts = piks.Select(p => p.Count(c => c == 0));

ps1. can i try use linq while debug?

Visual Studio doesn't support lambda expressions in the watch window.

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • With my every I see a different answer :) – L.B Oct 27 '11 at 20:23
  • Yes, byt what about debug? Can i write that code? While pasting that i cannot write further than `...p=>p` intelisense doesn't give any suggestion. I paste something like that: 'piksle_lista_tablic[84].Select(p => p.' and none suggesion. So.. can I use it in debug mode in watch? – deadfish Oct 27 '11 at 20:27
  • Not support, eh :( okay, thanks :P I though that if i write in debug it will work also in non :X – deadfish Oct 27 '11 at 20:28
  • 3
    @Cooldown4seconds: The quickwatch window sucks for Linq, you won't be able to use it to help you write Linq queries except in very limited situations. Not sure, but the immediate window might have the same problem, though you could try it. You can also try [LinqPad](http://www.linqpad.net/) instead, though it doesn't give you Intellisense unless you shell out cash. – Merlyn Morgan-Graham Oct 27 '11 at 20:34
1

If you want to count the global total, you can do this:

piks.SelectMany(p => p).Count(p => p == 0);

For each array you can do this:

piks.Select(p => p.Count(p => p == 0));
Miguel Angelo
  • 23,796
  • 16
  • 59
  • 82
1
List<byte[]> piks;
// Fill piks...
int zeroValuesCount = 0;
foreach (var pik in piks) {
    zeroValuesCount += pik.Count(x => x == 0);
}
Otiel
  • 18,404
  • 16
  • 78
  • 126