score:4
Accepted answer
well, if you wanted to, you could of course write an indexofmaxby
extension yourself.
example(untested):
public static int indexofmaxby<tsource, tprojected>
(this ienumerable<tsource> source,
func<tsource, tprojected> selector,
icomparer<tprojected> comparer = null
)
{
//null-checks here
using (var erator = source.getenumerator())
{
if (!erator.movenext())
throw new invalidoperationexception("sequence is empty.");
if (comparer == null)
comparer = comparer<tprojected>.default;
int index = 0, maxindex = 0;
var maxprojection = selector(erator.current);
while (erator.movenext())
{
index++;
var projecteditem = selector(erator.current);
if (comparer.compare(projecteditem, maxprojection) > 0)
{
maxindex = index;
maxprojection = projecteditem;
}
}
return maxindex;
}
}
usage:
var indexofpointwithhighestitem2 = mylist.indexofmaxby(x => x.item2);
score:5
it looks like there is a findindex
method defined on list
that would be perfect for this:
double max = mylist.max(t => t.item2);
int index = mylist.findindex(t => t.item2 == max);
Source: stackoverflow.com
Related Query
- How can I get LINQ to return the index of the object which has the max value in a collection?
- How can I get LINQ to return the object which has the max value for a given property?
- Which query has better performance to return an object which has the max value for a specific property?
- How can I make my Linq select return values if the value selected is null?
- LINQ Query How to select Max value between start and end index and the index of the max value
- How can I use Linq to build a c# object from xml where an element has zero or more elements of the same type?
- How can I return a list or enumerable of rows where column value has changed with Linq
- Using Linq to object, how can I get one field's value based on another in the same list
- Using LINQ, where one object property has a value of x, how do I return the value in a different property
- Linq code to get the index of an object in an array from an object within a list
- How to detemine if sequence has items and if so return the value in LINQ
- How to get data which has Max Date in linq
- How can I get the record which its value is the maximum
- how to get the value member of a selected comboBox to use it to add a new object with linq
- How can I use LINQ to get the instance with the highest value of a property in a child list?
- Linq: How can I get a result in a grouping query which is value agnostic and has dynamic columns?
- How can I get the value of an attribute for a method that has duplicates?
- How to find the index which matches the specfic value of IEnuemerable value using Linq in C#?
- How to get the index of a string value in a list, and assign it to a field in linq
- How to perform .Max() on a property of all objects in a collection and return the object with maximum value
- How can I get the index of an item in a list in a single step?
- How can I filter a dictionary using LINQ and return it to a dictionary from the same type
- How do I get the MAX row with a GROUP BY in LINQ query?
- How do I get the max ID with Linq to Entity?
- How do I get the index of the highest value in an array using LINQ?
- How to get linq `ForEach` statement to return data on the method call being made for each list object?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- How can I access the loop index inside a LINQ select?
- When using a LINQ Where clause on a Dictionary, how can I return a dictionary of the same type?
- Using LINQ to get DataGridView row index where first column has specific value
More Query from same tag
- what would be the equivalent this for loop in linq
- IQueryable - how to get all unique Years from db set?
- How to find value of "CourseCode" that is greater than 200 in XML document
- Linq: Split contents of a string column and add it to the array returned by Select(o=>o.ColumnName)
- Cannot implicitly convert type IQueryable to ObjectQuery with nested collections
- merge several lists but keep only values that are found on all the list
- Include or exclude where clause in Linq to Sql c# function
- How to filter a list with varying parameters in LINQ
- Linq based generic alternate to Predicate<T>?
- How to populate complex model with linq when datatables are different
- Query to filter contacts with given tags while having many to many relation between entities
- c# Lambda query in list that has a list inside for mapping
- SQLite, Linq, Foreach: how to optimize performances?
- using the equals keyword in linq
- Operator '!=' cannot be applied to operands of type 'bool?' and 'int'
- Cross Database Join with Linq - Updating T4 template to support DB name?
- Strange T-SQL / LINQ Performance issue when paging
- many to many Entity Framework GetUsersInRole()
- Specified cast is invalid when using firstordefault
- One To Many LINQ Joins Across Nested Collections
- NHibernate.Linq Query Issue with adding seconds to DateTime
- Finding duplicate items then selecting one with closest date to currentDate
- Using LINQ to obtain dependencies of tasks stored in a table
- Group objects into fixed-sized collections
- How do I return a default value if collection is empty?
- VS2013 Debugger + Entity Framework: "runtime has refused to evaluate the expression", crashes
- Convert datatable to list with relation
- Grouping objects into a period of time to produce minimal groups
- Group Duplicate Items with Linq while adding totals
- FormatException when using multiple Include() methods in MySQL Linq Query EF6