Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use C# to rank the results

Shulou Source: shulou.com Published: 2022-06-02 10:24:11 10月02日 Update

This article shows you how to use C# statistical results ranking, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

Public class Consult

{

Public string Name {get; set;}

Public int Times {get; set;}

Public int Rank {get; set;}

} var consults = new List ()

{

New Consult () {Name = "Zhang San", Times = 1}

New Consult () {Name = "Li Si", Times = 1}

New Consult () {Name = "Wang Wu", Times = 2}

New Consult () {Name = "Zhao Liu", Times = 3}

New Consult () {Name = "Xiaoming", Times = 0}

New Consult () {Name = "Xiao Hong", Times = 0}

}

We have got the number of times and names of all doctors consulted, and then we continue to operate them through LINQ. The most important thing to understand here is that projection (Select or SelectMany) has an index parameter, and everything will be easy to know if there is an index, because the higher the number of times, the higher the ranking, so first we need to reverse the above sets, and then group the passing times of the results after the reverse order. Grouping will result in different times in different sets (otherwise, the same number of times in the same set). Then the specified collection and index after grouping are obtained through SelectMany. Finally, the final result ranking is obtained by projecting the collection. The code is as follows:

Var result = consults.OrderByDescending (d = > d.Times)

.GroupBy (d = > d.Times)

.SelectMany ((g, I) = > g.Select (e = > new Consult ())

{

Name = e.Name

Times = e.Times

Rank = I + 1

}). ToList ()

The above content is how to use C# statistical results to rank, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Number of times results index grouping statistics differences reverse order content parameters skills knowledge projection same concise important and then concise code doctor Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno vpn Shulou Information Huawei MySQL