Domanda

Ho alcuni elementi chiamati ticker che può essere creato e sottoscritto.Quando vengono creati, hanno un ambito 1:privato o 2:pubblico.Chiunque può iscriversi ai ticker pubblici e chiunque può iscriversi ai ticker privati ​​su invito del proprietario.

Voglio utilizzare LINQ per ottenere i ticker creati da un utente (ovvero:con un determinato ID proprietario) che [1] sono pubblici [2] sono stati iscritti dall'utente corrente.

TickerUser       Ticker
==========       ======
TickerUserId <-- TickerId
TickerId         OwnerId
UserId           ScopeId

public IQueryable<Ticker> GetTickersByOwnerAll(int ownerId) {
    return from ticker in db.Tickers
           where ticker.OwnerId == ownerId
           select ticker;
}
/// THIS PART IS INCORRECT -- DO NOT USE
public IQueryable<Ticker> GetTickersByOwnerVisible(int ownerId) {
    int currId = CurrentUser.GetCurrentUser().ID;
    return GetTickersAll(ownerId)
           .Where(t => t.ScopeId == 2)
           .Where(t => t.TickerUsers.Where(tu => tu.UserId == currId));
}

L'ultima riga non è corretta ma puoi vedere dove stavo cercando di arrivare.Sono abbastanza nuovo in LINQ, quindi non ci penso ancora nel modo giusto.Dove sbaglio?

È stato utile?

Soluzione

db.Tickers.Where(t =>
    // The ticker is public...
    (t.ScopeId == 2)
    // ...or any of the ticker users is the current user.
    || t.TickerUsers.Any(tu => tu.UserId == currId))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top