2013-05-04 227 views
0

我是.NET世界的新手,我試圖從LINQ中獲得一些結果。請幫忙。無法將類型'System.Linq.IQueryable <AnonymousType#1>'隱式轉換爲'System.Collections.Generic.List

以下是我收到的錯誤。

錯誤4不能將類型'System.Linq.IQueryable'隱式轉換爲'System.Collections.Generic.List'。一個顯式轉換存在(是否缺少強制轉換?)..Business \ DAL \ GlobalRequest.cs 39個27個商業

public ObservableCollection<AccountSummary> GetAccountSummary() 
    { 
     ObservableCollection<AccountSummary> list; 
     list = from ListData in 
          (from a in dbc.Accounts 
          join b in dbc.Ledgers on a.Account_ID equals b.Account_ID into t2 
          from t2Data in t2.DefaultIfEmpty() 
          where a.Is_Mannual == Convert.ToChar("Y") 

          select new 
          { 
           Account_ID = a.Account_ID, 
           Account_Name = a.Account_Name, 
           Amount = t2Data.Amount == null ? 0 : t2Data.Amount 
          } 
         ).OrderBy(item => item.Account_Name) 
         group ListData by new 
         { 
          ListData.Account_ID, 
          ListData.Account_Name 
         } into GroupData 
         select new 
         { 
          Account_ID = GroupData.Key.Account_ID, 
          Account_Name = GroupData.Key.Account_Name, 
          Amount = GroupData.Sum(OL => OL.Amount) 
         }; 

    } 

class AccountSummary 
{ 
    public decimal AccountID { get; set; } 
    public string AccountName { get; set; } 
    public decimal Amount { get; set; } 
} 

請指點。

回答

1

您的查詢投影到匿名類型的集合(序列):

select new 
    { 
     Account_ID = GroupData.Key.Account_ID, 
     Account_Name = GroupData.Key.Account_Name, 
     Amount = GroupData.Sum(OL => OL.Amount) 
    }; 

您將需要投影到的AccountSummary集合,例如:

var accountSummaries = 

    ...rest of the query here 

select new AccountSummary 
    { 
     AccountId = ..., 
     AccountName = ..., 
     etc. 
    }; 

然後,您可以創建從這個集合可觀察收集AccountSummary

list = new ObservableCollection(accountSummaries); 
+0

它工作感謝幫助。偉大的保存 – user2198989 2013-05-04 10:06:17

相關問題