score:194
int keyindex = array.findindex(words, w => w.iskey);
that actually gets you the integer index and not the object, regardless of what custom class you have created
score:0
int index = -1;
index = words.any (word => { index++; return word.iskey; }) ? index : -1;
score:1
this solution helped me more, from msdn microsoft:
var result = query.asenumerable().select((x, index) =>
new { index,x.id,x.firstname});
query
is your tolist()
query.
score:2
just posted my implementation of indexwhere() extension method (with unit tests):
http://snipplr.com/view/53625/linq-index-of-item--indexwhere/
example usage:
int index = mylist.indexwhere(item => item.something == someotherthing);
score:3
try this...
var key = words.where(x => x.iskey == true);
score:7
if you want to find the word you can use
var word = words.where(item => item.iskey).first();
this gives you the first item for which iskey is true (if there might be non you might want to use .firstordefault()
to get both the item and the index you can use
keyvaluepair<wordtype, int> word = words.select((item, index) => new keyvaluepair<wordtype, int>(item, index)).where(item => item.key.iskey).first();
score:10
int keyindex = words.takewhile(w => !w.iskey).count();
score:66
for arrays you can use:
array.findindex<t>
:
int keyindex = array.findindex(words, w => w.iskey);
for lists you can use list<t>.findindex
:
int keyindex = words.findindex(w => w.iskey);
you can also write a generic extension method that works for any enumerable<t>
:
///<summary>finds the index of the first item matching an expression in an enumerable.</summary>
///<param name="items">the enumerable to search.</param>
///<param name="predicate">the expression to test the items against.</param>
///<returns>the index of the first matching item, or -1 if no items match.</returns>
public static int findindex<t>(this ienumerable<t> items, func<t, bool> predicate) {
if (items == null) throw new argumentnullexception("items");
if (predicate == null) throw new argumentnullexception("predicate");
int retval = 0;
foreach (var item in items) {
if (predicate(item)) return retval;
retval++;
}
return -1;
}
and you can use linq as well:
int keyindex = words
.select((v, i) => new {word = v, index = i})
.firstordefault(x => x.word.iskey)?.index ?? -1;
Source: stackoverflow.com
Related Query
- Find index of a value in an array
- How to find index of a value in array of char?
- LINQ to find array indexes of a value
- Convert an array to dictionary with value as index of the item and key as the item itself
- How do I get the index of the highest value in an array using LINQ?
- Find multiple index in array
- c# - LINQ find 2D jagged array minimum, return index
- Selecting index of array that has a value larger than a threshold
- How can I concatenate the array content with its index value
- Find closest value in an array List with linq?
- Find the List index of the object containing the closest property value
- Use linq to find index position, but take into account ties that depend on a value
- C# find JSON value based only on Key name through multiple levels of array
- Find index of item from string array containing comma separated values
- How to find the index of an element in an array inside a lambda expression in c#
- init float array incrementing index value every x times with Linq
- Add index position Value of an array of array using Linq
- LINQ orderby int array index value
- Linq code to get the index of an object in an array from an object within a list
- LINQ - Find index of a list item that has a null value
- Find index from an array using regex with linq in c# .net
- Find all elements from index modulo X with specific value in linq
- How to find out if one element in array has false value for property and if all elements have true value using Linq
- Find max value in List<int[]> by specific field in the array
- Find index of array with specific length in a List?
- List or Array of String Contain specific word in Html Source Code
- Get value and index first low value in array
- Best way to find if a value is present in the array, and if so execute code
- Find first index for value in List
- Join Two Array based on Index and Index Value using Linq Query Expression Syntax
More Query from same tag
- Linq to DataTable query missing the first record in result set
- Selection with where clause using generics C#
- How can I return an anonymous type from a method?
- How to generate a lambda at runtime passing a property name as string?
- Can I use LINQ to check if objects in a list have a unique ID?
- Making 1 table join on 2 other tables with LINQ
- Get the index of an element in a List that satisfies a condition
- xml XDocument from isolatedstorage, for use with LINQ
- linq group by statement not working
- Skip and take method in List
- Assign Values from one list to Another list based on Id
- ALiasing fields in linq
- How to convert to Linq
- .net MVC, SelectLists, and LINQ
- LINQ nested select causes multiple SQL queries
- Linq: Group the results of several GroupBy
- Merge attributes of two xml with same structure in c#
- LINQ Update or Insert objects into List
- Randomize but exclude the random variable
- LINQ to SQL variable number of columns for Select
- How to do Guid.TryParse in Linq query
- How do I join more than one files, in a Linq (C#) where one of the joins is a left join
- sort columns of gridview in asp.net c#
- Is there a other way to Group or Merge a separated Collection of List<T> and store the List<T> to the index of new List<T>?
- Select multiple columns with linq to sql
- How do I get the SQL command text when I insert a new record with Linq?
- optimise code into linq expression
- How to get PEX to automatically generate inputs for code involving LINQ
- Get record index column in linq select
- Last and LastOrDefault not supported