score:6
You can use Enumerable.DefaultIfEmpty
and specify a custom default value:
var result = componentData.Where(n => !string.IsNullOrEmpty(n.CompName))
.DefaultIfEmpty(componentData.ElementAtOrDefault(0))
.First();
Note that you can now use First
safely since the sequence cannot be empty anymore. ElementAtOrDefault
will return default(T)
(null for reference types) if the source sequence is empty. This will prevent an exception when you use the indexer of an IList<T>
directly.
score:2
var tt = componentData.FirstOrDefault(n=!string.IsNullOrEmpty(n.CompName)) ?? componentData[0];
Although you would want to check that componentData
was not null and count
/length
was greater than zero first too.
score:11
Take the line you currently have and add ?? componentData[0]
at the end.
??
is the null coalescing operator. It's just shorthand for, "if what's to my left is null, return what's on my right. If it's not null, return that."
Source: stackoverflow.com
Related Query
- LINQ Source Code Available
- creating Linq to sqlite dbml from DbLinq source code
- source code for LINQ 101 samples
- c# Linq or code to extract groups from a single list of source data
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Linq code to select one item
- How are people unit testing code that uses Linq to SQL
- Performance of LINQ Any vs FirstOrDefault != null
- foreach + break vs linq FirstOrDefault performance difference
- LINQ FirstOrDefault check for default value
- How to handle NULL object property with FirstOrDefault using Linq
- FirstOrDefault behavior directly in LINQ statement
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Safely dereferencing FirstOrDefault call in Linq c#
- Syntax to execute code block inside Linq query?
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Best open source LINQ provider
- Is there a good source that gives an overview of linq optimizations?
- Does this LINQ code perform multiple lookups on the original data?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- LINQ WHERE method alters source collection
- Linq FirstOrDefault
- Where can I view LINQ source code?
- Suggestions for designing complex LINQ code
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Left outer join using LINQ -- understanding the code
- How to pass LinQ Expressions from F# to C# code
- How to reuse a linq expression for 'Where' when using multiple source tables
- Avoiding code repetition when using LINQ
More Query from same tag
- LINQ... ignore nulls for dropdown C#
- Elegant solution - delete and re-add related child list as a batch with Linq-To-SQL
- Linq - select where ancestors contain this?
- TypedParameterExpression with Raven DB Any query
- How can I return a Dictionary<int, object>?
- union of different types using linq2db
- How to sum the ordinal value of letters in a string
- DbContext timeout while running linq query
- How do I update a foreign key efficiently in LINQ to SQL/SQLMetal?
- LINQ Select Records X Minutes apart
- Why can't I use a where clause on both tables in a LINQ Group Join?
- finding the count in two lists
- How to use EXISTS in where condition of LINQ?
- linq: separate orderby and thenby statements
- Reading list of ints from XML file using Linq
- retrieve only first occurrence of element with contain
- Implement EntitySet as IList
- Remove all elements of sublist except the last
- Will Linq ToList() generate NEW items?
- Can you use a List to configure a selector in LINQ join query?
- updating datatable column based on values of another column in group
- c# Search For XML Element Value Based On Array or List
- Why am I NOT getting a Null Reference Exception?
- No data return when performing a self join
- from datatable to array، no loop
- convert sql to linq
- dynamic join based on where expression - linq/c#
- Exception: when trying to use Enumerable.Any Method with Mobile Services query
- Entity Framework 6: virtual collections lazy loaded even explicitly loaded on a query
- dynamic linq contains