[Reserved] in C # using the Count method to get a collection List the number of eligible

List collection process often operate in, we need to query specific conditions, access to the collection List the number of physical objects that match the query, such as a group of objects List collection of products, if the number of defective batch of product is more than 10 Key notes. In C #, you can write your own for loop to determine the conditions to achieve one by one, in fact, there are kinds of easier way is to use the Count method to achieve the Lambda expressions, often you need only one statement.

For example, there is a collection of entities List studentList represent all students of aggregate data, we need to find out the code for the class that is equal to the number of ClassCode A101 class of students. At this time, the following statement can be implemented using Lambda expressions.

int A101StudentCount = studentList.Count (t => t.ClassCode = "A101");
in the above statement t is a lambda expression writing, on behalf of the collection entity object list, you can also write a or b.

In the above expression, you can also use the appropriate method in the lambda expression in parentheses, such as Find in a class all A101 family name is the number of students of Liu.

int A101StudentCount=studentList.Count(t=>t.ClassCode=“A101”&&t.StudentName.StartWith(“刘”));

Note: The text reproduced from personal bloggers station IT technology small fun house , the original link is in C # using the Count method to obtain the number of qualified technical _IT little fun house List collection .

Guess you like

Origin blog.csdn.net/weixin_39650424/article/details/93380510