I have made a Linq query where I am grouping by calls and summing duration and everything is working according to expectations. However problem is, that I am getting too many results as an output. After grouping I would like to get lets say maximum 10 top results and combine rest to "rest" group. Is there some way of doing that using linq?
Here is an example of my current query:
IEnumerable<DataModel> data = this.Data
.GroupBy(g => #here is regex match#)
.Select(s => new DataModel
{
CallerNumber = s.Key,
Duration = TimeSpan.FromTicks(s.Sum(c => c.Duration.Ticks)),
})
.Where(w => !string.IsNullOrEmpty(w.CallerNumber))
.OrderByDescending(o => o.Duration);
I can use .Take(10);
in the end of query, but how to combine all the leftovers to "rest" part? I mean I will get top 10 records, but then there are more at position 11, 12, 13, 14 etc. that are now not displayed. How to get them combined and displayed as 11th item in the output list.
IEnumerable<DataModel> data = this.Data
.GroupBy(g => #here is regex match#)
.Select(s => new DataModel
{
CallerNumber = s.Key,
Duration = TimeSpan.FromTicks(s.Sum(c => c.Duration.Ticks)),
})
.Where(w => !string.IsNullOrEmpty(w.CallerNumber))
.OrderByDescending(o => o.Duration)
.Take(10);
EDIT:
The idea is that I have, lets say phone numbers:
050321 4:30
045345 2:00
050321 4:00
045345 6:00
076843 1:00
050321 1:00
032345 3:00
043453 2:00
032345 3:00
This is what I would like to achieve (lets say I want to get top 3 and the rest combined):
050321 9:30
045345 8:00
032345 6:00
Rest 3:00