我有一个包含ProjectID
及其详细信息的类,如下所示,我想从SQL Server中检索列表详细信息。我正在努力将行列表转换为ProjectModel
。
我在下面尝试过,并引用了各种来源,但没有运气。我想编写获取列表的代码,而不是 BuildAutoCompleteList(PTO)。
我是否需要使用groupby
来获得结果。
课程
public class ProjectModel
{
public string ProjectName { get; set; }
public Guid ProjectID { get; set; }
public IEnumerable<SegmentModel> Segments { get; set; }
}
public class SegmentModel
{
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public string MileStone { get; set; }
}
public class ProjectMilestone
{
public string ProjectName { get; set; }
public Guid ProjectID { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public string MileStone { get; set; }
public ProjectMilestone() { }
}
实体
var collection = _db.GetReportMilestones().Join(_db.GetReports(), RM => RM.ReportId, R => R.Id,
(RM, R) => new { RM, R }).
Join(_db.GetProjects(), P => P.R.ProjectId, PR => PR.Id, (P, PR) => new { P, PR }).Where(M => M.P.R.StateId == 1)
.Select(PTO => new ProjectTimelineModel
{
ProjectName = PTO.PR.Name,
ProjectID = PTO.PR.Id,
Segments = BuildAutoCompleteList(PTO)
}).ToList();
下面是我的尝试,会引发错误。
var collection = _db.GetReportMilestones().Join(_db.GetReports(), RM => RM.ReportId, R => R.Id,
(RM, R) => new { RM, R }).
Join(_db.GetProjects(), P => P.R.ProjectId, PR => PR.Id, (P, PR) => new { P, PR }).Where(M => M.P.R.StateId == 1)
.Select(PTO => new ProjectMilestone
{
ProjectName = PTO.PR.Name,
ProjectID = PTO.PR.Id,
StartDate = PTO.P.RM.ScheduledDate,
EndDate = PTO.P.RM.PassedDate,
MileStone = PTO.P.RM.Name
}).ToList();
var results = collection.OrderBy(n => n.ProjectName);
var groupbyResult = results.GroupBy(n => new { n.ProjectID, n.ProjectName }).Select(x => new ProjectTimelineModel()
{
ProjectID = x.Key.ProjectID,
ProjectName = x.Key.ProjectName,
Segments = x.GroupBy(n => n.ProjectID)
}).ToList();