-1
我對此表示年月性能視圖模型:如何重構此屬性的代碼? C#
public IEnumerable<SelectListItem> Years
{
get
{
return new SelectList(
Enumerable.Range(1900, 112)
.OrderByDescending(year => year)
.Select(year => new SelectListItem
{
Value = year.ToString(CultureInfo.InvariantCulture),
Text = year.ToString(CultureInfo.InvariantCulture)
}
), "Value", "Text");
}
}
public IEnumerable<SelectListItem> Months
{
get
{
return new SelectList(
Enumerable.Range(1, 12)
.OrderByDescending(month => month)
.Select(month => new SelectListItem
{
Value = month.ToString(CultureInfo.InvariantCulture),
Text = month < 10 ? string.Format("0{0}", month) : month.ToString(CultureInfo.InvariantCulture)
}
), "Value", "Text");
}
}
當然我在這裏使用複製粘貼&辦法)我如何重構這個代碼? Mabby以某種方式將數字作爲參數傳遞給一些輔助方法?
您可能對http://codereview.stackexchange.com更好的運氣/ – cadrell0