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:9
Check out IEnumerable<T>.Any()
:
public bool HasSerialNumber()
{
if(this.Properties != null)
return Properties.Any(p => !string.IsNullOrEmpty(p.SerialNumer));
return false;
}
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));
Source: stackoverflow.com
Related Articles
- 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
- Dynamic Create Func<T,TR> C#
- Using LINQ, how can I limit rows retrieved depending on a child table property?
- Can someone explain LINQ's intersect properly to me? I don't understand why this doesn't work
- what is "if (predicate(item, index))" operation in LINQ?
- Delete Older Duplicate record from Datatable C#
- Return multiple attributes of an element to a list. LINQ to XML
- Hashset vs. IQueryable
- How to inject OR condition in Entity Framework linq query?
- is this tricky in linq? Regex with Linq combo
- How do I modify this LINQ statement to match only if the search term "Label" is at the end of the string?
- Dynamic Array to MS SQL Database
- join 2 query results orderby two different values
- How can I get the index of an item in a list in a single step?
- Captured Variable IndexOutOfBounds
- How do I write an MVC Entity Framework select query from the controller using passed string arguments?
- Can I improve these Pagination extension methods?
- Passing null parameters into LINQ where clause
- How to get elements value with Linq To XML
- LINQ OrderBy. Does it always return the same ordered list?
- Null expression with dynamic LINQ