我想返回一個列表,其中包含與年齡分佈有關的數據(例如,對於年齡0-9,有10人,對於10-19,有7人等)如何通過分組得到年齡分佈列表
這是我的查詢: -
public List<ReportHelper> GetAgeDistribution(int userId)
{
var itemList = (from item in this.ObjectContext.TreeMembers
where item.UserId == userId
group (DateTime.Now - (item.Birthdate ?? DateTime.Now)).Days/365.25
by ((DateTime.Now - (item.Birthdate ?? DateTime.Now)).Days/365.25)/9);
List<ReportHelper> list = new List<ReportHelper>();
foreach (var item in itemList)
list.Add(new ReportHelper { Data = item.Count(), Label = item.Key + "-" + (item.Key + 9) });
return list;
}
當我運行此查詢我得到dbarithmeticexpression例外:
DbArithmeticExpression arguments must have a numeric common type.
我怎麼解決這個問題?我知道實體框架4無法將我的表達式轉換爲普通的舊sql。我怎樣才能暗示我想要做什麼?
感謝提前:)
除非出生日期是類型'日期時間'你不需要'item.Birthdate ??? DateTime.Now'因爲DateTime不可爲空。 – Zebi 2011-04-23 14:36:35
@Zebi: - BirthDate是DateTime?否則代碼將不會編譯。問題不是任何可空類型。問題是EF無法將Linq轉換爲普通的舊SQL – TCM 2011-04-23 14:45:59