我有以下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;}
}
然后在我的代码中我有List<Item> items
包含A LOT
类型Item
的项目,我的问题是您推荐的排序/过滤项目的最佳方式/做法在基于CreatedDate的列表中,针对以下场景:
P.S。如果我还要提及时间怎么样?喜欢之前/之后/之间的日期x时间y?
答案 0 :(得分:9)
您可以使用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);
您可以使用foreach
或类似ToList
的方法来执行查询并实现结果。
foreach(Item i in beforeDateX)
Console.WriteLine("Name:{0} CreatedDate:{1}", i.Name, i.CreatedAt);
答案 1 :(得分:2)
使用Linq:
var itemsBefore = items.Where(i => i.CreatedDate <= timeBefore);
var itemsAfter = items.Where(i => i.CreatedDate >= timeAfter);
var itemsBetween = items.Where(i => i.CreatedDate >= timeStart && i.CreatedDate <= timeEnd);
订购
var ordrered = items.OrderBy(i => i.CreatedDate);
答案 2 :(得分:2)
考虑到你有一个List<>
,我建议:
List<Item> itemsBefore = items.FindAll(i => i.CreatedDate <= timeBefore);
List<Item> itemsAfter = items.FindAll(i => i.CreatedDate >= timeAfter);
List<Item> itemsBetween = items.FindAll(i => i.CreatedDate >= timeStart && i.CreatedDate <= timeEnd);
我建议的内容和其他建议之间存在细微差别。
.Where
方法不会“缓存”返回的列表,所以如果你这样做:
var filtered = items.Where(condition);
foreach (var item in filtered)
{
}
foreach (var item in filtered)
{
}
您的整个列表将被解析两次以搜索使条件成立的项目。要解决此“问题”(有时可能会出现问题),您可以在.ToList()
.Where()
List<>.FindAll()
仅返回包含所选项目的新List<>
。所以你可以枚举它想要多少次,因为它已经“物化”了。
答案 3 :(得分:1)
您可以使用LINQ Where:
static void Main(string[] args)
{
Item item1 = new Item() { CreatedDate = new DateTime(2010, 11, 10), Id = "1", Name = "foo1", Price = "10.00" };
Item item2 = new Item() { CreatedDate = new DateTime(2010, 11, 11), Id = "2", Name = "foo2", Price = "11.00" };
Item item3 = new Item() { CreatedDate = new DateTime(2010, 11, 12), Id = "3", Name = "foo3", Price = "12.00" };
Item item4 = new Item() { CreatedDate = new DateTime(2010, 11, 13), Id = "4", Name = "foo4", Price = "13.00" };
Item item5 = new Item() { CreatedDate = new DateTime(2010, 11, 14), Id = "5", Name = "foo5", Price = "14.00" };
Item item6 = new Item() { CreatedDate = new DateTime(2010, 11, 15), Id = "6", Name = "foo6", Price = "15.00" };
Item item7 = new Item() { CreatedDate = new DateTime(2010, 11, 16), Id = "7", Name = "foo7", Price = "16.00" };
Item item8 = new Item() { CreatedDate = new DateTime(2010, 11, 17), Id = "8", Name = "foo8", Price = "17.00" };
List<Item> items = new List<Item>();
items.Add(item1);
items.Add(item2);
items.Add(item3);
items.Add(item4);
items.Add(item5);
items.Add(item6);
items.Add(item7);
items.Add(item8);
List<Item> filtered = ItemsBeforeDate(items, new DateTime(2010, 11, 16));
foreach (Item i in filtered)
{
Console.Write(i.Name);
}
Console.Read();
}
public static List<Item> ItemsBeforeDate(List<Item> items, DateTime beforeDate)
{
return items.Where(i => i.CreatedDate < beforeDate).ToList();
}
public static List<Item> ItemsAfterDate(List<Item> items, DateTime afterDate)
{
return items.Where(i => i.CreatedDate > afterDate).ToList();
}
public static List<Item> ItemsBetweenDates(List<Item> items, DateTime startDate, DateTime endDate)
{
return items.Where(i => i.CreatedDate >= startDate && i.CreatedDate <= endDate).ToList();
}
打印:
foo1 foo2的 foo3 foo4 foo5 foo6
答案 4 :(得分:0)
您需要查看Enumerable METHODS
对于过滤使用Where
list.Where(x=>x.CreatedDate < yourDate).ToList();
list.OrderBy(x=>x.CreatedDate).ToList();
答案 5 :(得分:0)
但是如果你不想要过滤器,你可以在检索列表时实现分页。因为如果您设置较大的日期范围,则无法解决性能问题。
答案 6 :(得分:0)
所有LINQ方法都很棒,但它们迭代列表3次。如果确实有很多项目,那么老式的方式可能会更有效率(也就是说,如果你想同时使用所有三个场景,那么LINQ答案是可行的方法):
List<Item> before = new List<Item>();
List<Item> after = new List<Item>();
List<Item> between = new List<Item>();
foreach (var item in Items)
{
if (item.CreatedDate <= timeBefore)
{
before.Add(item);
}
else if (item.CreatedDate >= timeAfter)
{
after.Add(item);
}
else
{
between.Add(item);
}
}