我有以下Items类:
public class Item
{
public Item()
{
}
public string Id {get; set;}
public string Name {get; set;}
public string Price {get; set;}
public DateTime CreatedDate {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
然后在我的代码中我List<Item> items
包含A LOT
了类型的项目Item
,我的问题是你推荐的最佳方法/做法,根据CreatedDate对列表中的项目进行排序/过滤以用于以下场景:
PS如果我还要提及时间呢?喜欢之前/之后/之间的日期x时间y?
你可以使用LINQ
:
var beforeDateX = items
.Where(i => i.CreatedDate.Date < DateX); // remove the .Date if you want to include the time
var afterDateX = items
.Where(i => i.CreatedDate.Date > DateX);
var betweenDates = items
.Where(i => i.CreatedDate.Date >= DateX && i.CreatedDate.Date <= DateY);
Run Code Online (Sandbox Code Playgroud)
您可以使用foreach
或类似的方法ToList
来执行查询并实现结果.
foreach(Item i in beforeDateX)
Console.WriteLine("Name:{0} CreatedDate:{1}", i.Name, i.CreatedAt);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8266 次 |
最近记录: |