WebUsing LINQ. items.Max (v => v.Value) Items containing greatest value. var max = items.Max (v => v.Value); var x = items.Where (v => v.Value == max); However, MaxBy extension method suggested by devdigital will iterate the collection only once. Share. … WebApr 17, 2024 · List nums = new List {0.2, 5.0, 12.0}; and I am trying to find the element for which the element-element^2 is greatest. I can run nums.Max (num => num - Math.Pow (num, 2)) to find the maximum but how can I then get the index of this element that identifies as yielding the greatest value when evaluating num - Math.Pow …
C# find biggest number - Stack Overflow
WebThe following example demonstrates the find methods for the List class. The example for the List class contains book objects, of class Book, using the data from the Sample XML File: Books (LINQ to XML). The FillList method in the example uses LINQ to XML to parse the values from the XML to property values of the book objects. Web1 Answer Sorted by: 125 With EF or LINQ to SQL: var item = db.Items.OrderByDescending (i => i.Value).FirstOrDefault (); With LINQ to Objects I suggest to use morelinq extension MaxBy (get morelinq from nuget): var item = items.MaxBy (i => i.Value); Share Improve this answer Follow edited Dec 3, 2024 at 14:33 matao 636 3 14 22 free clipart shepherd with sheep
c# - The most efficient way to get object with max value from list …
WebMay 23, 2012 · To Get/Set using reflection you need an instance. To loop through the items in the list try this: PropertyInfo piTheList = MyObject.GetType ().GetProperty ("TheList"); //Gets the properties IList oTheList = piTheList.GetValue (MyObject, null) as IList; //Now that I have the list object I extract the inner class and get the value of the property ... WebJan 28, 2013 · Instead of calling Min or Max directly on the collection returned by our Where statement, we can Select () the property list we are interested in. Then we can slip in DefaultIfEmpty. Calling Min () or Max () on DefaultIfEmpty ensures us a … Webvar value = MyList.First (item => item.name == "foo").value; (This will just find the first match, of course. There are lots of options around this.) Or use Find instead of FindIndex: var value = MyList.Find (item => item.name == "foo").value; I'd strongly suggest using LINQ though - it's a much more idiomatic approach these days. free clip art shipping trunks