score:36
you could do something like this:
ienumerable<person> persons = ..
var firstpersonafterjack = persons.skipwhile(p => p.name != "jack")
.elementat(1); //zero-indexed, means second
the idea is to produce a sequence resulting in skipping elements until you meet the condition, then take the second element of that sequence.
if there's no guarantee that the query will return a result (e.g. a match is never found, or is the last element of the sequence), you could replace elementat
with elementatordefault
, and then do a null
-test to check for success / failure.
i notice you say in your question that you have an ordered list of people. if you could explain what that means in more detail, we might be able to provide a better answer (for example, we may not have to linear-search the sequence).
score:0
you can use code like this:
string todir = environment.getcommandlineargs().skipwhile(x => x != "/to").skip(1).take(1).firstordefault();
this value gets == null
if "/to"
command line argument not given, non-null
if path was provided.
score:5
skipwhile
is a method that takes a predicate and skips everything until the predicate is false. it returns that element and everything after.
var remainingpeople = collectionofpeople.skipwhile(p => !istheperson(p));
if (remainingpeople.count() == 1)
{
// the person was the last in the list.
}
var nextperson = remainingpeople.skip(1).first();
where istheperson
is a method that takes a person and returns true if it is the person you are interested it.
Source: stackoverflow.com
Related Query
- Using Linq to find the element after a specified element in a collection
- How to find the index of next matching element in list using LINQ
- Get next element from Query after element with specified Id. The LINQ expression could not be translated
- VB:NET using LINQ how can i find the row index for a specified string in some Datatable column
- How do I find the text within a div in the source of a web page using C#
- Using LINQ to Objects to find items in one collection that do not match another
- using LINQ to find the cumulative sum of an array of numbers in C#
- Find first element of certain type in a list using LINQ
- Linq to SQL - How to find the value of the IDENTITY column after InsertOnSubmit()
- Get new indices of items in a collection after sorting using LINQ
- Appending an element to a collection using LINQ
- Access all of the data after joining two tables and group them using linq
- Using Linq to find duplicates but get the whole record
- Add quotes at the beginning and end of each element in list using LINQ
- Find the Second Max in a list of values using linq c#
- How to get all elements except the n'th element in a List using Linq
- Using LINQ to find all keys from one collection that are not in another?
- How to find the first item according to a specific ordering using LINQ in O(n)?
- Left outer join using LINQ -- understanding the code
- Using LINQ to delete an element from a ObservableCollection Source
- Using LINQ to get the results from another LINQ collection
- I need a LINQ expression to find an XElement where the element name and attributes match an input node
- How can I write the following code more elegantly using LINQ query syntax?
- How do I use LINQ to find 5 elements in a row that match one predicate, but where the sixth element doesn't?
- How to get the value of an XML element using Linq even when empty
- Using Linq to find next element in subset of a list
- Find the newest item in a C# generic list using Linq
- How to apply a function to every element in a list using Linq in C# like the method reduce() in python?
- Find indices of particular items in the list using linq
- How do I find an XML element by attribute using LINQ to XML?
More Query from same tag
- Mongodb c# driver: view MQL bson query generated from linq
- The entity or complex type 'CreditFacility_Web.Models.CreditFacilityModel.SavingsAccount' cannot be constructed in a LINQ to Entities query
- Convert List of Tree View nodes to combined record
- The LINQ expression node type 'ArrayLength' is not supported in LINQ to Entities
- LINQ to XML - How does it work?
- Convert DataTable into list when no of column is dynamic
- Proper Searching with Linq, Entity Framework and Database Indexes
- Unable add an entity that already exists
- db4o native query in C#: how to find by first number?
- linq query Unable to create a constant value collection within collection
- Computational and stylistic optimal solution for the conditional where clause using Linq
- Combine List of like DataTables
- Joing multiple Linq Clauses
- Cannot make 5 consecutive "joins" in LINQ?
- How to select multiple tables using LINQ
- Select query with subquery that can return List<string>
- Data is ignored at ThenInclude() for nested List property
- Threading and IEnumerable; "Collection Was Modified"-exception
- Updating your edmx to reflect changes made in your db (.net linq-to-entities)
- How to properly sort a string-number column of a DataTable
- Using a 'where' statement in Linq only when a variable has a value
- Lambda expression in IF condition
- Given a sequence, find the largest 'reverse-ordered' subsequence
- Linq2SQl eager load with multiple DataLoadOptions
- Problems ordering my collection with LINQ
- Generic custom linq filter
- How to perform sql join on Dictionary<string,List<string>>?
- How do I create subsets of one list from one condition in c#
- Build string for Linq WHERE clause depending on multiple checkbox choices
- Creating Extension method to include subquery