score:12
You can use Any
instead of checking if the count is greater than zero.
return Properties.Any(x => !string.IsNullOrEmpty(x.SerialNumber))
and of course your Properties.Count > 0
check is redundant.
score:3
This should do the trick:
return Properties.Any(x => !string.IsNullOrEmpty(x.SerialNumber));
score:6
I don't think you'll improve particularly on the performance of string.IsNullOrEmpty()
, but one pitfall you should be avoiding is the last 2 calls on your query - specifically ToList()
and Count()
.
What you are doing there is iterating through every element, converting it to a list (creating a list and adding items in the process, and then iterating through every element on the list to count how many there are - all to check if a single value is empty.
You can use the Any
method to find if a single element matches certain criteria, like so:
return Properties.Any(x => !string.IsNullOrEmpty(x.SerialNumber));
score:9
Check out IEnumerable<T>.Any()
:
public bool HasSerialNumber()
{
if(this.Properties != null)
return Properties.Any(p => !string.IsNullOrEmpty(p.SerialNumer));
return false;
}
Source: stackoverflow.com
Related Query
- LINQ approach to this code
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Does this LINQ code perform multiple lookups on the original data?
- LINQ Source Code Available
- How does this linq code that splits a sequence work?
- multiple orderby in this linq code
- How can I combine this code into one or two LINQ queries?
- How can I further simplify this piece of LINQ code
- Is there a bug in this code from 101 LINQ Samples on MSDN? (Update: Fixed)
- How to render this map-reduce javascript code to an equivalent LINQ Select-Aggregate?
- creating Linq to sqlite dbml from DbLinq source code
- How do i convert this linq code to inline sql
- why does this linq code get exponentially slower when applying First() to projection?
- How to convert this recursive code to Linq
- code first approach error: the specified type member 'yyyxx' is not supported in linq to entities
- Using Linq to build a graph class; can you make this code look better?
- How to write this code using the Linq Extension Method-Syntax?
- Convert this LINQ code back to a Loop (or Why is this object sometimes null)
- How can I refactor this code for LINQ filtering?
- Why this Linq code always throws System.StackOverflowException?
- My code is very inefficient for this simple Linq usage
- How I change this code to be in linq style
- 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?
- Linq - how to convert this code to linq
- How best to optimise this small bit of c# Linq code
- how to write LINQ to objects equivalent code for this code
- Reduce the line of code for this LINQ query
- Can I can convert this C# code into some Linq code?
- How to handle nulls in this LINQ Code using a possible null List?
- source code for LINQ 101 samples
More Query from same tag
- Is there any benefit in using Predicate<T> over building a query manually?
- get keys of KeyValuePair List to int[] array using linq
- Deserialize SOAP XML Response
- How to group items by index? C# LINQ
- DataStax C# Driver for Apache Cassandra for Materialized View
- Is necessary to compile linq queries in subsonic?
- LINQ: grouping collection
- How can I force EF Core to convert LINQ's .Contains() to EF.Functions.Like()?
- get common prefix of two string
- Update single table from a Model with referential constraint without modifying referenced table
- Convert stored procedure to Linq statement
- ExpandoObject with Select Clause
- LINQ OrderBy Count of Records in a Joined Table
- Getting an error when i am changing my folder location
- Query range of data linq c#
- Passing F# function to IEnumerable.Where vs IEnumerable.All
- C# - boolean property value wont change :(
- Entity Framework 4.3 - Polymorphic Query With Eager Loading
- how to write linq query with where clause to get records between 9 am to 5 pm
- How to get a note node having particular hashtag value
- Exposing a protected collection for LINQ queries
- LINQ query grouping by column in child table and ordering the results within the groups
- Slow LINQ operation
- NH Linq with FetchMany and ToFutureValue in NH 3.0.0beta
- CRM 2013 - linq query to get accounts for a specific owner
- C# LINQ Help with Errors
- Multiple records using Find method in Entity Framework by passing list of primary keys
- How to merge n List<Dictionary<string, string>> into a Dictionary using Linq
- How to count the number of dates in a dateTime list
- C# How to sort a list of objects by any property