[英]ASP.NET Core MVC Calculate percentage of ENUM
如何計算 ENUM 的出現百分比? 我有一個預訂清單,我的 ENUM 中有 2 種不同類型的 sockets。 例如,如果 booking_1 有 type_1,booking_2 有 type_1,然后 booking_3 有 type_2 我應該在我的視圖表中使用 type_1 66.6% 和 type_2 33.3%
這是我的枚舉:
namespace WebApplication.Models
{
public enum ConnectorType
{
[Display(Name = "Type 2-Plug")]
Type2Plug,
[Display(Name = "CCS Combo 2-Plug")]
CCSCombo2Plug
}
}
這是視圖模型:
namespace WebApplication.ViewModels
{
public class ConnectorTypeEvaluationViewModel
{
[Display(Name = "Anschlusstyp")]
public ConnectorType connectorType { get; set; }
[Display(Name = "Prozentsatz")]
public double connectorPercentage { get; set; }
}
}
我的問題是 Controller。 我不知何故需要使用循環來計算我的 ENUM 類型在我的 bookingsList 中每次出現的百分比,但我不知道如何:
namespace WebApplication.Controllers
{
public class ConnectorTypeEvaluationController : Controller
{
private IMemoryCache _cache;
public ConnectorTypeEvaluationController(IMemoryCache cache)
{
_cache = cache;
}
public IActionResult Index()
{
List<Booking> bookingsList;
_cache.TryGetValue("key", out bookingsList);
double total = bookingsList.Count;
connectorType = ??;
connectorPercentage = ??;
return View();
}
}
}
據我了解,您想計算所有預訂的每個連接器類型的百分比。 使用這兩個連接器創建一個列表並計算百分比如下:
List<ConnectorTypeEvaluationViewModel> connectorTypeEvaluationViewModel = new List<ConnectorTypeEvaluationViewModel>();
connectorTypeEvaluationViewModel.Add(new ConnectorTypeEvaluationViewModel
{
connectorType = ConnectorType.Type2Plug,
connectorPercentage = (bookingsList.Count(s => s.connectorType==ConnectorType.Type2Plug)/Convert.ToDouble(bookingsList.Count()))*100
});
connectorTypeEvaluationViewModel.Add(new ConnectorTypeEvaluationViewModel
{
connectorType = ConnectorType.CCSCombo2Plug,
connectorPercentage = (bookingsList.Count(s => s.connectorType== ConnectorType.CCSCombo2Plug) / Convert.ToDouble(bookingsList.Count())) * 100
});
百分比計算為(i / n) * 100
,其中n
是觀察總數, i
是 Type2Plug 的數量。
可能會以某種方式進行優化,也許是通過使用GroupBy
。
public IActionResult Index()
{
List<Booking> bookingsList;
_cache.TryGetValue("key", out bookingsList);
double type2PlugCount = bookingsList.Count(x => x.connectorType == ConnectorType.Type2Plug);
double cCSCombo2PlugCount = bookingsList.Count(x => x.connectorType == ConnectorType.CCSCombo2Plug);
double total = bookingsList.Count;
var type2PlugPercentage = (type2PlugCount / total) * 100;
var cCSCombo2PlugPercentage = (cCSCombo2PlugCount / total) * 100;
return View();
}
編輯
我創建了一種基於屬性計算百分比的通用方法
public static Dictionary<TProperty, double> Percentage<T, TProperty>(this IEnumerable<T> source, Expression<Func<T,TProperty>> selector)
{
var dict = new Dictionary<TProperty, double>();
Func<T,TProperty> func = selector.Compile();
var list = source.ToList();
var groups = list.GroupBy(func).ToList();
double total = list.Count;
foreach(var group in groups)
{
double groupCount = group.Count();
var percentage = (groupCount / total) * 100;
dict.Add(group.Key, percentage);
}
return dict;
}
用法
var dict = list.Percentage(x => x.SomeProperty);
非常感謝您的幫助。 我設法創建了一個 foreach 循環來添加每種類型,所以如果我需要更多類型,我不必手動添加所有類型:這是代碼:
namespace WebApplication.Controllers
{
public class ConnectorTypeEvaluationController : Controller
{
private IMemoryCache _cache;
public ConnectorTypeEvaluationController(IMemoryCache cache)
{
_cache = cache;
}
public IActionResult Index()
{
List<Booking> bookingsList;
_cache.TryGetValue("key", out bookingsList);
List<ConnectorTypeEvaluationViewModel> connectorsList = new List<ConnectorTypeEvaluationViewModel>();
var connectors = Enum.GetValues(typeof(ConnectorType)).Cast<ConnectorType>();
foreach (var item in connectors)
{
connectorsList.Add(new ConnectorTypeEvaluationViewModel
{
connectorType = item,
connectorPercentage = (bookingsList.Count(s => s.connectorType == item) / Convert.ToDouble(bookingsList.Count())) * 100
});
}
return View(connectorsList);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.