L
L
Ledington2021-12-14 15:56:27
C++ / C#
Ledington, 2021-12-14 15:56:27

How to count the percentage of elements in a list with LINQ?

Tell me how you can calculate the percentage using LINQ and display its result?
There is a list of values, it can be different in number. It is necessary to display, for example, 30 percent of this list, that is, [9,8,7] (33%). Rounding up.

public void Start()
{
    var list = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    list.Top(30)
}

public void Top(int list)
{
    IEnumerable<int> topPercent = ;
}


It must also be limited by the number of elements in the output collection. Maximum 100, otherwise throws an exception.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2021-12-14
@Ledington

list.Take(list.Count() * 30 / 100)
I think with a check for 100 and an exception you will figure it out yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question