這是一個ASP.NET MVC項目。目前,我有一個表,如:將列轉置爲行並將日期行轉換爲列
Skill Date Offered Answered Abandoned
SampleSkill1 12/1/2013 53585 52549 1036
SampleSkill2 12/1/2013 7170 6997 173
SampleSkill1 11/1/2013 45635 45189 446
SampleSkill2 11/1/2013 6481 6378 103
SampleSkill1 10/1/2013 54838 54208 630
SampleSkill2 10/1/2013 7361 7235 126
我試圖獲取數據表中的一樣:
SampleSkill1
Type Oct Nov Dec
Offered 53585 52549 1036
Answered 45635 45189 446
Abandoned 54838 54208 630
我已經能夠獲得最接近的是做這樣的事情這個:
var StatsList = db.Stats.Where(c => c.Skill == "SampleSkill1").ToList();
var OfferedStatsQuery = StatsList
.GroupBy(c => c.Skill)
.Select(g => new StatsPivot {
Skill = g.Key,
Oct = g.Where(c => c.Date.Month == 10).Sum(c => c.Offered),
Nov = g.Where(c => c.Date.Month == 11).Sum(c => c.Offered),
Dec = g.Where(c => c.Date.Month == 12).Sum(c => c.Offered)
});
var AnsweredStatsQuery = StatsList
.GroupBy(c => c.Skill)
.Select(g => new StatsPivot {
Skill = g.Key,
Oct = g.Where(c => c.Date.Month == 10).Sum(c => c.Answered),
Nov = g.Where(c => c.Date.Month == 11).Sum(c => c.Answered),
Dec = g.Where(c => c.Date.Month == 12).Sum(c => c.Answered)
});
我相信這不是做這種事情的最好/正確的方法,但我我對LINQ很陌生。有關如何有效實現預期結果的任何建議?