score:14
Try:
strings.FirstOrDefault(s=>s.StartsWith("J"));
And also if you are new to LINQ I'd recommend going through 101 LINQ Samples in C#.
score:2
bool hasJName = strings.Any(x => x.StartsWith("J"));
This checks to see if any names that start with J exist.
string jName = strings.FirstOrDefault(x => x.StartsWith("J"));
This returns the first name that starts with J. If no names starting with J are found, it returns null
.
score:2
Using the First
LINQ method (in System.Linq
):
strings.First(e => e.StartsWith("J"));
Or FirstOrDefault
if you are not sure that any element in your list will satisfy the condition:
strings.FirstOrDefault(e => e.StartsWith("J"));
Then, it returns null
if no element has been found.
score:7
You can use FirstOrDefault
:
var firstMatch = strings.FirstOrDefault(s => s.StartsWith("J"));
if(firstMatch != null)
{
Console.WriteLine(firstMatch);
}
Source: stackoverflow.com
Related Articles
- How to find first occurrence in c# list without going over entire list with LINQ?
- How to implement SkipWhile with Linq to Sql without first loading the whole list into memory?
- How to find first occurrence with LINQ
- Find first element of certain type in a list using LINQ
- Check if IEnumerable has ANY rows without enumerating over the entire list
- Linq with where clause in many-to-many EF Code First object
- check if record is last or first in list with linq
- Find top-level parent in List with LINQ
- LINQ query over a list of Bool+String pairs. How to join strings without foreach?
- Using Linq find first object in list sorting by property A, then property B
- C# - Linq optimize code with List and Where clause
- Linq sub query when using a repository pattern with EF code first
- c# find item in list returned by LINQ query and compare its value with another item in list
- LINQ get find all with id in list
- Order a list with unique rows with a given row as first item, then reorder the list but without the first row
- Match first character of a word in a String List with Linq
- LINQ query with Left Join, Group by without null List
- iterating over columns in a list of tuples with Linq
- How to find the first parent in a tree-like hierarchy using LINQ with EF Core
- Find string with most frequency of a character in List of strings using LINQ C#
- Find a dupe in a List with Linq
- Efficient way to replace int in list with first occurrence index
- Iterate with LINQ over a list and set founded varible
- Comparing a list of CSV values in Linq to match those from second list with any value in first list
- how to write a Linq query with a EF code first Many to Many relationship
- Linq find first value of list in other list
- C# LINQ Find List Inside Another List, Better way to code this than a foreach loop
- How to filter object list with LINQ without using nested If statement
- How to change some range values in entire list with Linq
- LINQ query to find objects in list with equal values for one of their properties
- How do I get summ in Linq query?
- Is there some sort of syntax error with this LINQ JOIN?
- C# Joining 3 Lists with Linq
- Can we remove the repeating string elements here? Linq C#
- Convert lambda expression to Json
- Grouping by a boolean property does not work in EF Core
- Problem with retrieving data from xml by linq
- Domain Model + LINQ to SQL sample
- How to get Sum from List in another List as a property with linq
- WhereSelectEnumerableIterator to data table c#
- EF Return Aggregated and Non-Aggregated Data
- how to get max time from a report
- Linq deferred execution
- LINQ: dynamic Where clause with toggle the combination of cases
- Linq conversion
- Is Linq to objects join default order specified when no order by is used?
- Element 'group' does not match any field or property of class Gillie.JobCenter.Domain.KeyValueEntity
- Linq - multiple rows as string
- What makes "SqlMethods" Methods Work?
- Translating query comprehension to Enumerable extension methods in LINQ