c # in the list set using Max () method to find the maximum value

List C # in the set operation, sometimes need to find the set of maximum List, then the method can be extended using the method of List collection Max, Max method has two forms, one is without any formal parameters, apply List value type to a number of variables set, the other is written with a Lambda expression form, this method is applicable to obtaining a maximum value of a set of attribute List.

1. Max form without any parameters of the example, the program calls the following form:

List<int> list1 = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var maxValue = list1.Max();

 

Operation results: maxValue = 10.

2. With the form of written expression Lambda Max way of example

We need to get List <TestModel> testList maximum collection of objects in a collection of object properties Index, first look at the definition of TestModel:

public class TestModel
 {
        public int Index { set; get; }

        public string Name { set; get; }
 }

Get all objects testList Index property set maximum value with the following statement:

List<TestModel> testList = new List<ConsoleApplication1.TestModel>();
var max = testList.Max(t => t.Index);

Reproduced in: https: //www.50bit.cn/News/Index/6395.html

 

Guess you like

Origin www.cnblogs.com/liuping666/p/12011072.html