在LINQ的最近3个月

用户名

我在这样的存储过程中有这行:

DATENAME(MONTH, tblReg.StartDate) as [Month],

现在我要在linq中转换此行

var b = sd.tblReg;

foreach (var c in b)
{
    res += "'" + c.StartDate + "',";
}

res = res.Substring(0, res.Length - 1);
res += "]";

并希望获得过去3个月..即当前月份为8月,所以与8月相同,如果当前月份为1月,然后是12月11月,则我希望获得最近3个月。

['May' ,'June','July','Aug']
哈里·普拉萨德(Hari Prasad)

您可以执行以下操作来查找以前的3月份LinqDateTimeFormat.GetMonthName将帮助您获取月份名称。

int month = ..; // given a month
var result = Enumerable
    .Range(-2,18)                  // Compute +/- 3 months for original 12 months.
    .TakeWhile(x=>x <=month)       // Take months until the current month
    .Reverse()                     // Reverse the order as we need backword months.
    .Take(4)                       // Take top 4 months (including current month)
    .Select(x=>CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x<=0?x+12: x==12? 12 : (x+12)%12))

检查一下 Demo

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章