I am trying to populate Transaction data where AccountNumber does not exist. I need to access the Account table to get that. I am getting the following error where I am trying to return IEnumerable
Cannot implicitly convert type System.Collections.Generic.IEnumerable<AnonymousType#1> to System.Collections.Generic.List<ProjectModel.Transaction>
The error is shown on top of .ToList(); part of the code. What am I doing wrong?
the code is:
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;
}
List of anonymous types cannot be casted to list of transactions. Looks like your
Transactionclass do not haveAccountNumberproperty. Also you cannot return anonymous objects from methods. So you should create some type which will hold required data:And return these objects:
BTW you don’t need duplicate join condition in where filter