2013-02-12 35 views
5

Sto tentando di compilare i dati di transazione in cui AccountNumber non esiste. Devo accedere alla tabella Account per ottenerlo. Sto ottenendo il seguente errore in cui sto cercando di tornare IEnumerableImpossibile convertire implicitamente il tipo 'System.Collections.Generic.IEnumerable <AnonymousType # 1>' a 'System.Collections.Generic.List <modelClass>

Impossibile convertire implicitamente il tipo System.Collections.Generic.IEnumerable<AnonymousType#1> a System.Collections.Generic.List<ProjectModel.Transaction>

L'errore viene visualizzato sulla parte superiore del ToList(); parte del codice. Che cosa sto facendo di sbagliato?

il codice è:

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; 

    } 

risposta

5

L'elenco di tipi anonimi non può essere convertito in elenco di transazioni. Sembra che la tua classe Transaction non abbia la proprietà AccountNumber. Inoltre non è possibile restituire oggetti anonimi dai metodi. Così si dovrebbe creare un certo tipo, che conterrà dati richiesti:

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

E restituire questi oggetti:

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(); 
    } 
} 

BTW non è necessario duplicato unirsi condizione in cui il filtro

+1

Questo ha funzionato perfettamente. Ottima risposta Grazie mille per avermi messo in direction.Doing proprio in questo che ho imparato poche altre cose. Ora so di cosa tratta ViewModel. – shaz

2

Il tipo anonimo, che in fase di proiezione nella sezione "selezionare nuova" della query LINQ non può essere colato direttamente al vostro tipo "Operazione".

Invece, è necessario proiettare una nuova istanza di Transazione. Il seguente potrebbe aiutare:

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(); 
+0

Questo sarebbe non funziona perché non ho la proprietà AccountNumber nella tabella delle transazioni. – shaz

+0

Forse elencerai anche le proprietà della tua classe poco la prossima volta? :) – RainbowFish

+0

Questa è sicuramente una buona idea, sicuramente la ricorderò la prossima volta. Grazie per l'aiuto però. – shaz

Problemi correlati