score:8
int[] a = {1, 2, 3, 0, 5, 0};
int x = a.Where(b => b != 0).Count();
score:6
Use Where
.
int x = a.Where(b => b != 0).Count();
The Select() projects each element of a sequence into a new form.
score:3
change this line
int[] a = {1, 2, 3, 0, 5, 0};
int x = a.Select(b => b != 0).Count();
to
int[] a = {1, 2, 3, 0, 5, 0};
int x = a.Where(b => b != 0).Count();
Differnce between
Where
finds items that match and only returns those that do.
-> IEnumerable<A>
in, IEnumerable<A>
out
Select
returns something for all items in the source. That something might be the items themselves, but are more usually a projection of some sort.
-> IEnumerable<A>
in, IEnumerable<B>
out
score:2
Your code is just projecting the collection to bools and you get true where the value is not 0 and false where it is, to filer user where
e.g.
int[] a = {1, 2, 3, 0, 5, 0};
int x = a.Where(b => b != 0).Count();
score:7
Others have suggested using Where
followed by Count
, but you can do it even more simply:
int x = a.Count(b => b != 0);
This overload of Count
takes a predicate, and basically only counts the elements of the source sequence which match the predicate.
As others have noted, the reason it doesn't work for Select
is that that just projects - it doesn't filter. You could use:
// Works but is horrible...
int x = a.Select(b => b != 0).Count(z => z);
... but I'd really suggest you don't.
Note that none of this does what your title talks about, which is finding positive values. For that, you'd want:
int x = a.Count(b => b > 0);
score:2
Your mistake is that {1, 2, 3, 0, 5, 0}.Select(b => b != 0)
is just {true, true, true, false, true, false}
, which is again 6 items. You can better do with Where
instead, which would give you the filtered sequence: {1, 2, 3, 5}
. For the resulting sequence you can apply Count
or whatever.
Source: stackoverflow.com
Related Articles
- How to count the number of elements that match a condition with LINQ
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- How do I count the number of child collection's items using LINQ Method Syntax?
- count number of identical elements in two arrays in linq
- Count number of given object in a list with LINQ
- How do I use LINQ to count number of objects in largest group?
- LINQ Source Code Available
- Count the number of children in my JSON file using JSON.NET with LINQ
- Get all +ve number count in LINQ
- LINQ to count number of rows in link table
- creating Linq to sqlite dbml from DbLinq source code
- Count the number of Enum values in list using Linq
- Count number of Mondays, Tuesdays etc from table using Linq
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- Why the extension method of where for LINQ in this code would print out a single number while it shouldn't print anything at all?
- Count the number of Files modified in the last hour using linq
- Count number of records for a certain condition with linq
- Use LINQ to count the number of combinations existing in two lists
- source code for LINQ 101 samples
- NHibernate LINQ count number of groups in query
- Count number of Attempts made using Linq
- Linq count number of differences in lists in a collection
- I want to count number of each element in list using linq in c#
- How do I count the number of records with no match on the right side of a Linq left join?
- elegant and efficient linq solultion to count number of episodes
- Linq query to select distinct record and count number of distinct record
- How do I count the number of items > 0 in a Dictionary<string, int> using Linq in C#/.Net?
- LINQ to SQL - Count number of certain item types in each order
- how to count the number of items contains in list in C# or linq
- c# Linq or code to extract groups from a single list of source data
- Linq to XML Simple Query
- Binding DefaultExpression to property in Linq-to-Entities query
- Summing up property values using LinQ
- linq To XML update values
- Linq query giving inappropriate output
- C# LINQ - How to build Group By clause dynamically
- How to create a non-nullable CRM basic type like int or double?
- working with IQueryable; not working
- Getting Total Record by Group by in Linq
- Converting SQL statement to LinQ to SQL query
- Get the next enumerated item after applying a predicate via Linq
- update statement not working for linq
- CreateDocumentQuery() with property other than id
- SQL, combining two tables for query
- Try something 5 times with Rx
- Entity Framework ToListAsync() with Select()
- How to append an array to a List using LINQ?
- Exception when converting local realm to synced realm in Xamarin.iOS
- How to create a LINQ expression from an Entity Framework navigation property?
- "Case" in the order-by statement