我能夠產生一組結果是可取的,但我有需要組和這些領域的總和,並正在努力理解如何處理這個。如何聚集和多SUM的EntityFramework的字段加入
我的方案,會是什麼,得到的結果,將最好的辦法:
- 有一個獨特的[邀請碼(現在我得到很多的記錄,同樣邀請碼 但不同職業的詳細信息)
- SUM工資和投影領域(在相同的查詢)
這裏是我的LINQ代碼:
private IQueryable<MyAbstractCustomOccupationInfoClass> GetMyAbstractCustomOccupationInfoClass(string[] regionNumbers)
{
//Get a list of wage data
var wages = _db.ProjectionAndWages
.Join(
_db.HWOLInformation,
wages => wages.KeyCode,
hwol => hwol.KeyCode,
(wages, hwol) => new { wages, hwol }
)
.Where(o => regionNumbers.Contains(o.hwol.LocationID))
.Where(o => o.wages.areaID.Equals("48"))
.Where(o => regionNumbers.Contains(o.wages.RegionNumber.Substring(4))); //regions filter, remove first 4 characters (0000)
//Join OccupationInfo table to wage data, for "full" output results
var occupations = wages.Join(
_db.OccupationInfo,
o => o.wages.KeyCode,
p => p.KeyCode,
(p, o) => new MyAbstractCustomOccupationInfoClass
{
KeyCode = o.KeyCode,
KeyTitle = o.KeyTitle,
CareerField = o.CareerField,
AverageAnnualOpeningsGrowth = p.wages.AverageAnnualOpeningsGrowth,
AverageAnnualOpeningsReplacement = p.wages.AverageAnnualOpeningsReplacement,
AverageAnnualOpeningsTotal = p.wages.AverageAnnualOpeningsTotal,
});
//TO-DO: How to Aggregate and Sum "occupations" list here & make the [KeyCode] Distinct ?
return occupations;
}
我不確定是否應該在第二次連接時執行分組機制?或者執行.GroupJoin()?或者有第三個查詢?
我認爲'GroupJoin'會有用。 'OccupationInfo'中有一個條目在'工資'中是否有多個條目? – NetMage
@NetMage你是對的,每個OccupationInfo條目的工資有多個條目。 – AussieJoe