我有一个名为EventListingResponse
的模型对象
public class EventListingResponse
{
public List<events> result { get; set; }
public int totalResults { get; set; }
}
public class events
{
public string id { get; set; }
public string name { get; set; }
public string startDate { get; set; } //smaple date 2014-03-31T12:30:03
}
我正在将数据存储在此对象中
EventListingResponse eventListResponse = rGetAllEventList();//data from another function.
现在我要过滤此列表,以便我可以获得events
的列表,
- &gt; 1月份的活动
- &gt; 2月份的活动等
任何人都可以指出一个很好的方法来实现这个目标吗?
答案 0 :(得分:1)
您可以使用LINQ查询过滤列表:
var filtered = from item in eventListResponse.result
where DateTime.Parse(item.startDate) >= DateTime.Parse("your starting date")
and DateTime.Parse(item.startDate) <= DateTime.Parse("your ending date")
select item;
您还需要将数据分配从rGetAllEventList()
更改为eventListResponse.result
。
答案 1 :(得分:1)
定义您希望使用
过滤的月份字符串数组string[] monthresult = { "January", "March" };
使用Linq
var filteredresults = eventListResponse.result.Where(o => monthresult.Contains(Convert.ToDateTime(o.startDate).ToString("MMMM")));
filteredresults将列出1月和3月的事件列表
答案 2 :(得分:-1)
你可以试试这个:
EventListingResponse evl = new EventListingResponse();
evl.result = new List<events>();
evl.result.Add(new events() { id = "1", name = "test1", startDate = "2014-03-31T12:30:03" });
evl.result.Add(new events() { id = "2", name = "test2", startDate = "2014-03-31T14:30:03" });
evl.result.Add(new events() { id = "3", name = "test3", startDate = "2014-05-31T16:30:03" });
evl.result.Add(new events() { id = "4", name = "test4", startDate = "2014-05-31T12:30:03" });
var dated_EventListResponse = evl.result.Select(t => new { date = DateTime.Parse(t.startDate), MyEvent = t });
var grouped_EventListResponse = dated_EventListResponse.GroupBy(g => g.date.Month);
foreach (var group in grouped_EventListResponse)
{
Console.WriteLine("Filter: " + group.Key);
foreach (var grouped_event in group)
{
Console.WriteLine("\t" + grouped_event.MyEvent.name);
}
}