Вы не можете сделать это с Entity Framework, но вы можете написать метод для перебора списка в памяти. Например, если у вас есть класс для хранения ключа и значения, как это (или вы можете переписать, используя KeyValuePair
или кортеж):
public class ItemCount
{
public string Name { get; set; }
public int Count { get; set; }
}
Метод расширения для агрегирования меньших значений может выглядеть следующим образом это:
public static IEnumerable<ItemCount> AggregateWithThreshold(this IEnumerable<ItemCount> source,
int threshold)
{
// The total item to return
var total = new ItemCount
{
Name = "Others",
Count = 0
};
foreach (var item in source)
{
if (item.Count >= threshold)
{
// If count is above threshold, just return the value
yield return item;
}
else
{
// Keep the total count
total.Count += item.Count;
}
}
// No need to return a zero count if all values were above the threshold
if(total.Count > 0)
{
yield return total;
}
}
И вы бы назвали это так:
var list = context.Ways
.GroupBY(s => s.Type)
.Select(w => new ItemCount // Note we are using the new class here
{
Name = w.key,
Count = (int)w.Sum(b => b.Length)
});
var result = list.AggregateWithThreshold(100);