2013-12-17 84 views
0

我試圖將每日日誌分組到一個從dbset linq查詢的月份,得到高於或低於允許使用的百分比。LINQ轉換匿名列表強烈類型與組中的日期分割

的dbset是

public partial class DailyUsageForYear 
{ 
    public System.DateTime DateAdded { get; set; } 
    public Nullable<long> NumUsers { get; set; } 
    public int AllowedUsageCount { get; set; } 
    public string Application { get; set; } 
    public System.Guid ApplicationID { get; set; } 
    public System.Guid SupplierID { get; set; } 
} 

我嘗試獲得對象的列表像

public class UsageDisplay 
{ 
    public int Year { get; set; } 
    public int Month { get; set; } 
    public string Application { get; set; } 
    public int NumUsers { get; set; } 
    public int AllowedUsageCount { get; set; } 
    public Guid ApplicationID { get; set; } 
} 

但是在發言的最後選擇似乎並不瞭解字段名,任何人都可以告訴我,我錯過了linq語法?

var predicate = PredicateBuilder.False<DailyUsageForYear>(); 
    predicate = predicate.And (x => x.NumUsers > (x.AllowedUsageCount * (DropPercentage/100))); 
if (supplier != null) 
{ 
    Guid supplierGuid = new Guid (supplier.ToString()); 
    predicate = predicate.And (x => x.SupplierID == supplierGuid); 
} 
if (Direction == 0) 
    predicate = predicate.And (x => x.NumUsers < (x.AllowedUsageCount * (Percentage/100))); 
else 
    predicate = predicate.And (x => x.NumUsers > (x.AllowedUsageCount * (Percentage/100))); 
usageDetailModel.usage = db.DailyUsageForYears.**Where**(predicate) 
    .**GroupBy**(x => new { x.DateAdded.Year, x.DateAdded.Month, x.Application, x.NumUsers, x.SeatCount, x.LicenceID }) 
    .**Select**(u => new UsageDisplay() { Year = u.DateAdded.Year, Month = u.DateAdded.Month, Application = u.Application, NumUsers = u.NumUsers, SeatCount = u.SeatCount, ApplicationId = u.ApplicationID }); 
+0

刪除在您的「選擇」中出現兩次的'.DateAdded'。匿名對象只拾取最後一個屬性名稱。 – Rawling

回答

0

內最後選擇uDailyUsageForYearKey領域的groupping對象,所以嘗試改變你的選擇這樣的

.Select(u => new UsageDisplay() { 
        Year = u.Key.Year, 
        Month = u.Key.Month, 
        Application = u.Key.Application, 
        NumUsers = u.Key.NumUsers, 
        SeatCount = u.Key.SeatCount, 
        ApplicationId = u.Key.ApplicationID 
       }); 

.GroupBy(x => new { 
     x.DateAdded.Year, 
     x.DateAdded.Month, 
     x.Application, 
     x.NumUsers, 
     x.SeatCount, 
     x.LicenceID }) 

我認爲你需要x.ApplicationId代替x.LicenceID

+0

這是問題的關鍵謝謝 – user2903089