Unable to implicitly convert the type 'System.Collections.Generic.IEnumerable <AnonymousType # 1>' to 'System.Collections.Generic.List <modelClass>

I am trying to populate Transaction data where AccountNumber does not exist. I need to access the Account table to get this. I get the following error when I try to return IEnumerable

Cannot implicitly convert type System.Collections.Generic.IEnumerable<AnonymousType#1>toSystem.Collections.Generic.List<ProjectModel.Transaction>

The error is shown on top of .ToList (); parts of the code. What am I doing wrong?

the code:

    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;

    }
+5
source share
2 answers

. , Transaction AccountNumber. . - , :

public class AccountTransaction
{
    public int LocalAmount { get; set; }
    public int AccountNumber { get; set; }
}

:

public static IEnumerable<AccountTransaction> GetAllTransactions()
{       
    using (var context = new CostReportEntities())
    {
        return (from t in context.Transactions
                join acc in context.Accounts 
                     on t.AccountID equals acc.AccountID              
                select new AccountTransaction {
                     AccountNumber = acc.AccountNumber,
                     LocalAmount = t.LocalAmount
                }).ToList();
    }
}

, ,

+5

, " " Linq, "".

. :

allTransactions = (from t in context.Transactions
    join acc in context.Accounts on t.AccountID equals acc.AccountID
    where t.AccountID == acc.AccountID
    select new Transaction()
    {
        AccountNumber = acc.AccountNumber,
        LocalAmount = t.LocalAmount
    }).ToList();
+2

All Articles