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 Query
- 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
- LINQ query over a list of Bool+String pairs. How to join strings without foreach?
- How to find the first parent in a tree-like hierarchy using LINQ with EF Core
- how to write a Linq query with a EF code first Many to Many relationship
- How to filter object list with LINQ without using nested If statement
- How to change some range values in entire list with Linq
- How to find first N items with Min values using LINQ
- How to find the second greatest element in a list with LINQ
- How can I find the first items in a list with a different value for a property using Linq?
- How to iterate in a list to find names and values with linq
- How to get list of parents and person in a recursive list with linq without children?
- How to find the first occurrence of a substring in a list of substrings?
- How to call an Sql User defined Function using Entity frame work Code first approach with LInq c#
- How to Count Duplicates in List 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
- How to update an element with a List using LINQ and C#
- How to use LINQ Contains() to find a list of enums?
- How can I find object in List with Linq?
- How to find point closest to 0,0 point with LINQ and C#
- How can I iterate over a collection and change values with LINQ extension methods?
- How to find the first item according to a specific ordering using LINQ in O(n)?
- How to find a match with 2 comma separated strings with LINQ
- How to update a single column in LINQ without loading the entire row?
- How to write a LINQ to Entities query with List in a "WHERE" condition
- How can I split a list vertically in n parts with LINQ
- How to remove first occurence of element in List C# with LINQ?
- How to convert list of objects with two fields to array with one of them using LINQ
More Query from same tag
- How to update value in a List using LINQ
- How to make IEnumerable<string>.Contains case-insensitive?
- Iterate and select over two dimensional string array with LINQ
- Nullable DateTime in Where Clause in EF Linq Query
- I'm trying to filter the response from the below controller. I dont want the variables to be included in the search if they are empty
- linq sorted string in array by sub string
- .net core: ICollection in model always empty
- How to compare a string column(as DateTime) in LINQ?
- Divide list in sublists by elements
- Can I perform Linq OrderBy with asynchronous calls in parallel?
- How to convert a loop to Linq
- Check if item exists in list and push value into it — in one shot
- Change value of label and hide imagebutton in asp repeater
- Parsing two consecutive records in XML file
- Filtering List Objects Based on Property String
- How to fix this "already an open DataReader associated with this Command which must be closed first" in my Linq-statement?
- XML LINQ statement almost giving desired results
- Linq extension to recursively retrieve collection of items
- Linq to XML - Create Xelement only IF something
- EF recursive hierarchy
- Get all the child details in a xml?
- Query Json Object dynamically
- SQL Join table and SUM values
- Create a new list in C# with LINQ, sorted by type and ignoring objects of the same type if they occur more than once
- Linq filter by several enumerators
- C# Comparison operators not supported for type Dictionary<string,string>
- Querying an auto-generated EF junction table
- Multiple joins using linq
- Does LINQ work on Index?
- Reflection Linq-Entity "IN()" function for "contains" searching group of integers