score:3
if (entity.count > 0)
or if (entity.any())
are identical in you case. as you already fetched all the data from the db, the list has been built and you knows its size. so .count
property doesn't iterate over anything.
in the other hand, do not call the .count()
ienumerable
extension if you didn't fetched all data, because it'll enumerate items for nothing.
use instead:
bool test = soleservice.all()
.any(s => (s.shoelastid == shoelastid) && (s.status == 20));
if (test)
{
...
}
also, linq extensions won't return null but an empty ienumerable
, so don't check for null
.
score:1
entity.any() is better choice. and you don't need to invoke .tolist() since this wll take all the data from the database and then just check its count.
score:2
if you have a .tolist()
call, then the list is always a list. maybe empty, but never null.
the check for .any()
instead of .count() > 0
is a performance improvement for most containers or enumerables because .any()
will only touch the first element if there is one. .count()
would need to count through your container to the end although you are not interested in the result, only in the fact that it's not zero.
score:2
depends on what you need.
if you just want to know if any of entities match your predicate then use any()
, as it will return immediately upon finding the first matching entity.
count() / count
will need to process all the entities which will typically be much slower.
also prefer linq's count()
to list count
as it doesn't have to create the full list in the memory, which can be very expensive with large result sets.
score:2
any()
will provide better solution cause it stopse after first matching.
in addition
i would suggest also to do tolist()
only if any()
is true.
youll save (micro) performance.
var t = soleservice.all() .where(s => (s.shoelastid == shoelastid) && (s.status == 20));
if (t.any()) entity =t.tolist();
score:2
entity.any() - will return true if there is any entities in your collection. entities.count() == 0 will do the same. but i would recommend to use any because it will work faster. because count will return the amount of data in the collection, but any will trigger on the first item found in your collection. but if you are not sure that your collection is initialized i would recommend you to use next construction:
if(entity!=null && entity.any())
{
//do something. you will get her always without error, and you will be 100% sure that your collection is not empty and it is initialized
}
hope it helps.
score:2
when you call if (entity.count > 0)
but entity == null
, you will get an exception because .count
does not exist while entity is not initialized.
score:2
of course a list can be null
or empty. if you attempt to create a list<sole>
using linq as above and the soleservice
could be null, in which case you will get a nullreferenceexception
. so i would check that the soleservice
is not null
or empty first. so
list<sole> entity = null;
// ...
if (soleservice != null && soleservice.count > 0)
entity = soleservice.all()
.where(s => (s.shoelastid == shoelastid) && (s.status == 20))
.tolist();
i hope this helps.
Source: stackoverflow.com
Related Query
- When to check List<T> for null, when for 0 and when both
- What is the most efficient way or best practice for null check when you use Split and FirstOrDefault methods together?
- Linq query, how to check for a null value and use the value 0 in place of a null?
- Linq check for null and replace null value in orderby
- How to unpack an expression tree and check for null values
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How to check string for null and assign its value in the same line
- Contains in LINQ query and Check for Not null
- How to check for null and empty string in Exression.Constant?
- Least code to convert one object to anothe for both single object and List<object>?
- lambda check for null and then set to 0
- Sequence contains no elements error but I want to check for null
- How to check for null before I use in linq?
- A Shortcut for c# null and Any() checks
- LINQ Any() and Single() vs. SingleOrDefault() with null check
- How to return null if using SingleOrDefault() and searching a list of numbers for a number not in list?
- When using "yield" why does compiler-generated type implement both IEnumerable and IEnumerator
- Check Contains into an int array for null property
- Linq where clause with multiple conditions and null check
- How to reuse a linq expression for 'Where' when using multiple source tables
- linq to sql left join, need to check for null for right table
- Check DateTime for null in linq expression
- How can I check for a null inside of a linq query?
- Avoid extra loop and could not find implementation of query pattern for source type int Select not found
- Why do linq search has a huge difference in efficiency when I use string and array especially for large data?
- Better Enumerable.Range for both ascending and descending ranges?
- Check if List is not null when using "contains" in LINQ query else select all records
- Using C# LINQ Expressions for both Value Types and Reference Types
- Issue with query returning null before I can check for null
- Code Example for Add DateTime and TimeSpan in EF query
More Query from same tag
- Find a particular item in an object tree using LINQ
- Working with 1-Result-Linq with For Each? Any alternative?
- How can you take only the last result with Linq
- Subtract a generic list from another
- how should I initialize IQueryable variables, before use a Union expression?
- Filter some unique Data with LINQ and C#
- Data grid View ( winform ) not populating properly
- C# linq Files.ReadAllLines() fails for large 650MB CSV file
- linqtosql will not allow updating of fields as it casts them as read only
- Sort Linq result in alphabetical order
- C# Linq expression in C++
- how group by in Linq with 2 Field?
- Convert Join and Group By from SQL to LINQ
- Group by with a select statement with Distinct list of Strings
- How to get records from tables in custom order of ids?
- Converting SQL to LINQ with Multiple Tables and Group
- How to do a All() using Dynamic LINQ
- Is there a better way to get the column values from datatable
- C# + LINQ - inner join
- Data filtering or better LINQ query?
- Contains inside Linq-to-Entities
- finding items in a list
- I am trying to add Linq result in Datatable but getting Error
- Returning Concatenated String with LINQ for Dropdown
- Delete with subsonic
- C# lambda null conditional
- How could I modify a LinQ query on the fly?
- Linq c# Querying based on a Variable
- Instantiating Immutable Objects With Reflection
- Group by in LINQ on a property in an array