Domanda

Perché sto ricevendo un'eccezione legante di runtime quando provo a eseguire la seguente query in enorme?

  public dynamic Find(string email, string type)
    {
        dynamic result = new ExpandoObject();
        result = this.Query(@"SELECT * FROM Addresses a 
            INNER JOIN Members m ON m.Id = a.MemberId 
            INNER JOIN AddressType at ON at.Id = a.AddressTypeId 
            WHERE m.Email = @0 AND at.Type = @1", new {email, type});
        return result;
    }

Modifica per mostrare la soluzione: dovevo cambiare la mia query per garantire che fosse restituita una sola colonna con il nome 'id'. Stavo ricevendo un errore di vincolo perché più colonne in membri e indirizzi avevano una colonna denominata "ID". Per ottenere un singolo risultato nella mia domanda ho dovuto modificarlo a questo:

result = this.Query(@"SELECT a.* FROM Addresses a 
            INNER JOIN Members m ON m.Id = a.MemberId 
            INNER JOIN AddressType at ON at.Id = a.AddressTypeId 
            WHERE m.Email = @0 AND at.Type = @1", new object[] { email, type }).Single();

Spero che questo aiuti qualcun altro.

È stato utile?

Soluzione

Stai passando gli Args in un oggetto anonimo, anziché un array di oggetti (parametri). Diventa quindi il primo argomento e non si lega a quando la tua query si aspetta 2 argomenti. Inoltre Query Tipo di restituzione IEnumerable<dynamic>.

Cambia il tuo codice in questo:

public dynamic Find(string email, string type)
{
    IEnumerable<dynamic> result;
    result = this.Query(@"SELECT a.* FROM Addresses a 
        INNER JOIN Members m ON m.Id = a.MemberId 
        INNER JOIN AddressType at ON at.Id = a.AddressTypeId 
        WHERE m.Email = @0 AND at.Type = @1", email, type);

    // decide how you want to handle multiple results here
    return result.FirstOrDefault();
}

Oppure potresti usare un array di oggetti espliciti:

        WHERE m.Email = @0 AND at.Type = @1", new object[] {email, type});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top