2015-12-22 55 views
0

我正在为电影院构建预订系统(当然,这只是用于研究的小型项目)。通过DateTime属性拆分对象列表

这里是我的展示模型

public class ShowcaseModel 
{ 
    public string objectId { get; set; } 
    public string MovieId { get; set; } 
    public int Auditorium { get; set; } 

    public DateTime? StartDate { get; set; } 
} 

我想在"per day"形式显示进度。要做到这一点,我得到的所有展柜,其中日期时间是比今天更大,使用起始日期属性付诸

List<ShowcaseModel>. 

现在我不知道如何分割该列表(成单独的列表)一天。

有什么办法可以达到这个目的吗?

在此先感谢。

回答

1

您可以使用GroupBy方法:

List<ShowcaseModel> list = new List<ShowcaseModel>(); 
//... 
var gooupByDay = list.GroupBy(o=>o.StartDate.Value.Date); 
1

我用夹具(简单地创建类的实例随机)来演示如何可以得到每个日期的项目清单。

var fixture = new Fixture(); 

IEnumerable<ShowcaseModel> showCaseModel = fixture.CreateMany<ShowcaseModel>(); 
IEnumerable<ShowcaseModel> futureShowCases = showCaseModel.Where(s => s.StartDate != null && s.StartDate > DateTime.Now); 

// we know none of start dates are null 
var groupedShowCases = futureShowCases.GroupBy(s => s.StartDate.Value.Date); 

List<Tuple<DateTime, IEnumerable<ShowcaseModel>>> showCasesByDate = new List<Tuple<DateTime, IEnumerable<ShowcaseModel>>>(); 
foreach (var groupedShowCase in groupedShowCases) 
{ 
    var key = groupedShowCase.Key; 
    showCasesByDate.Add(Tuple.Create(key, groupedShowCase.ToList().AsEnumerable())); 
} 
1

使用LINQ GroupBy()

var grouped = list.Where(f=>f.StartDate!= null) 
      .GroupBy(f => f.StartDate.Value.Date, b => b, 
           (k, g) => new { Date= k.Date, 
               Movies= g }).ToList(); 

假设list是你ShowCaseModel的集合