我想填充AccountNumber不存在的Transaction數據。我需要訪問帳戶表來獲取。我收到以下錯誤在那裏我試圖返回的IEnumerable無法將類型'System.Collections.Generic.IEnumerable <AnonymousType#1>'隱式轉換爲'System.Collections.Generic.List <modelClass>
不能隱式類型轉換System.Collections.Generic.IEnumerable<AnonymousType#1>
到System.Collections.Generic.List<ProjectModel.Transaction>
錯誤顯示在.ToList()的頂部;部分代碼。我究竟做錯了什麼?
代碼:
public static IEnumerable<Transaction>GetAllTransactions()
{
List<Transaction> allTransactions = new List<Transaction>();
using (var context = new CostReportEntities())
{
allTransactions = (from t in context.Transactions
join acc in context.Accounts on t.AccountID equals acc.AccountID
where t.AccountID == acc.AccountID
select new
{
acc.AccountNumber,
t.LocalAmount
}).ToList();
}
return allTransactions;
}
這工作完美。優秀的答案。非常感謝讓我走向正確的方向。做到這一點,我也學到了其他一些東西。現在我知道什麼是ViewModel。 – shaz 2013-02-12 21:33:52