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 Articles
- When to check List<T> for null, when for 0 and when both
- Check if List is not null when using "contains" in LINQ query else select all records
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- Linq query null check in data source
- What is the most efficient way or best practice for null check when you use Split and FirstOrDefault methods together?
- How to check if IEnumerable is null or empty?
- How to null check c# 7 tuple in LINQ query?
- Entity-framework code is slow when using Include() many times
- What is the best way to check IQueryable result set is null
- Sequence contains no elements error but I want to check for null
- LINQ: adding where clause only when a value is not null
- How to check for null before I use in linq?
- LINQ new instance when SingleOrDefault returns null
- Check if single() LINQ return NULL
- LINQ Any() and Single() vs. SingleOrDefault() with null check
- Null check String.ToLower in Linq Where expression
- Linq Select New List Property Null Check
- When using "yield" why does compiler-generated type implement both IEnumerable and IEnumerator
- Check if a list contains all of another lists items when comparing on one property
- Linq How to Get Average When all values equals null or 0 ? MVC
- LINQ SQL query check if a object field is not null
- 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
- check list string is null or empty
- XDocument.Element returns null when parsing an xml string
- Avoiding code repetition when using LINQ
- Linq to objects when object is null VS Linq to SQL
- Forcing Entity Framework to not generate NCLOB's when building Linq-to-Sql Code (Model First)
- linq to sql left join, need to check for null for right table
- How do you filter a list so that no member is a substring of another member
- Find the List index of the object containing the closest property value
- How can a session variable hold a record from database?
- How to insert a lambda into a Linq declarative query expression
- MVC Update Details Controller
- Entity Framework generate not expected query
- Search with more criteria in Lambda Expression between two lists
- Entity Framework 6.1 - how can you query soft deleted records?
- Linq with embedded functions
- Entity Framework 4: How to get records in many-to-many relationship?
- Why changes to the outer data-source is not reflected, while they show-up for the inner data-source?
- LINQ with groupby and aggregate always shows default value
- C# - Lambda - grouping List data containing nested List objects
- Filtering data in Linq c#
- Linq statement for Orders reporting
- Accessing entities with navigation properties using Linq in Web Api and EF
- linq query with or and operator together
- Lambda expression is slower than foreach statement?
- LINQ: Comparing specific values in two datatables
- Haven't jumped on the Linq bandwagon, what are your reasons for not using linq?