score:8
int[] a = {1, 2, 3, 0, 5, 0};
int x = a.where(b => b != 0).count();
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: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.
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:6
use where
.
int x = a.where(b => b != 0).count();
the select() projects each element of a sequence into a new form.
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);
Source: stackoverflow.com
Related Query
- Get all +ve number count in LINQ
- Linq query to get all numbers (positive and negative) up to N that sum up to number K
- How to get a count of all children lists, grandchildren lists, great grandchildren lists etc. in Linq
- Linq Order by a specific number first then show all rest in order
- Get indexes of all matching values from list using Linq
- 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?
- Get item count of a list<> using Linq
- LINQ - Get all items in a List within a List?
- Is it possible to use Linq to get a total count of items in a list of lists?
- How do I count the number of child collection's items using LINQ Method Syntax?
- Linq query to get count
- Get all pairs in a list using LINQ
- Why does LINQ query throw an exception when I attempt to get a count of a type
- count number of identical elements in two arrays in linq
- Trying to get all elements after first match using linq
- C# get minute number from date in linq group by
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- Linq How to Get Average When all values equals null or 0 ? MVC
- How to get all elements except the n'th element in a List using Linq
- LINQ - get total count of values from nested list
- Get the number of distinct property values in List<T> using LINQ in C#
- Count number of given object in a list with LINQ
- linq query to join two tables and get the count from one table values from the other
- Get Value and Count of that value using LINQ or lambda expression
- How do I use LINQ to count number of objects in largest group?
- Linq - Get the Index of the Last Non-Zero Number of Array
- Using LINQ to get all property with the exact same value?
- how to get all columns from a linq result with a join
- Get all possible distinct triples using LINQ
More Query from same tag
- Is there an equivalent of bulk contains?
- Calculate how much positions of 2 arrays contain equal elements
- LINQ to XML - How can I access inner tags in an XML efficiently?
- Exclude Students Ids for those whom have already checked in
- nested linq queries
- Linq Query Optimization in Entity Framework
- Linq query with a Conditional WHERE Clause
- Using IN subquery in Entity Framework using Linq
- Using LINQ to compare on a string value
- Filtering out values from a list of object in a List
- Handling the children of a parent-child relationship using Linq to XML
- How can I add a DisplayMember and ValueMember to a single ComboBox Item that has an assigned DataSource?
- Unable to find nested property using LINQ
- Emulating a join which uses a contains operator opposed to equals?
- Excuting query: "LINQ to Entitites does not recognize method 'System.Object getItem(System.String)'"
- LIKE query with underscore (_) using Entity Framework & Sqlite
- C# LINQ and XML Getting child nodes
- How can I switch that code to use LINQ
- Entity Framework Linq vb get list of users based on curent user account list or belong to no account
- Entity Framework - writing query using lambda expression
- Create a dictionary of a model?
- How I can pass the LINQ result anonymous type to another method?
- Ordering a linq list based on a property of a child class
- Iterating through a heterogenous container?
- The cast to value type 'System.Single' failed because the materialized value is null for stored procedure
- Left join on two Lists and maintain one property from the right with Linq
- Multiply decimal and a double in Linq
- Linq Query Involving Association Tables
- Error in Linq Pivot query
- which linq to use to get collection searching in the collection inside current collection