如何从列表中选择最近12个月的数据< Students>? [英] How Can I Select Last 12 Months Of Data From A List<Students>?
本文介绍了如何从列表中选择最近12个月的数据< Students>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个清单,
班级学生
{
public DateTime AdmissionDate { get ; set ; }
public string StuName { get 跨度>; set ; }
public string StuClass { get 跨度>; set ; }
public string StuAddress { get 跨度>; set ; }
}
列表<学生> allStudent = obj.AllStudentsData();
此allStudent中的
包含过去3年学生的数据到目前为止,
如何从中选择最近12个月的数据并列表
allStudent = allStudent(过去12个月的数据)从今天起);
使用linq到实体。
先谢谢你
解决方案
// 需要Linq
DateTime 12MonthsBeforeNow = DateTime.Now.AddMonths(-12);
列表< student> Last12MonthStudent = allStudent
。其中(student = > student.AdmissionDate > = 12MonthsBeforeNow)
.ToList();
你只需要两件事。首先,有一些时间点,你需要计算距离它12个月的另一个点。这是使用System.DateTime.AddMonths
完成的:
http://msdn.microsoft.com/en-us/library/system.datetime.addmonths%28v=vs.110%29.aspx [ ^ ]。
然后你需要能够定义某个时间点是否在某个给定的时间间隔内。您可以使用类型System.DateTime
:
http:// msdn.microsoft.com/en-us/library/System.DateTime%28v=vs.110%29.aspx [ ^ ]。
-SA
I have a list,
Class Students
{
public DateTime AdmissionDate{ get; set; }
public string StuName{ get; set; }
public string StuClass{ get; set; }
public string StuAddress{ get; set; }
}
List<Students> allStudent=obj.AllStudentsData();
in this allStudent contains data for students for last 3 years till now,
how can i select the last 12 months data from it and make a list
allStudent=allStudent(Last 12 month data from today);
using linq to entity.
Thanks in Advance
解决方案
// requires Linq DateTime twelveMonthsBeforeNow = DateTime.Now.AddMonths(-12); List<student> Last12MonthStudent = allStudent .Where(student => student.AdmissionDate >= twelveMonthsBeforeNow) .ToList();
You need just two things. First, having some point in time, you need to calculate another point 12 months away from it. This is done usingSystem.DateTime.AddMonths
:
http://msdn.microsoft.com/en-us/library/system.datetime.addmonths%28v=vs.110%29.aspx[^].
Then you need to be able to define if some point of time is withing some given interval. You can use the operators '==', '>', '<', '>=' and '<=' defined on the typeSystem.DateTime
:
http://msdn.microsoft.com/en-us/library/System.DateTime%28v=vs.110%29.aspx[^].
—SA
这篇关于如何从列表中选择最近12个月的数据< Students>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文