Pergunta

Eu tenho alguns itens chamados tickers , que pode ser criado e subscrito. Quando eles são criados, eles têm um alcance de 1: privado ou 2: public. tickers públicas pode ser subscrito por qualquer pessoa e tickers privadas pode ser subscrito por convite proprietário.

Eu quero usar LINQ para obter os tickers criada por um usuário (ou seja: com uma certa identificação de proprietário) que quer [1] são públicas [2] foram subscritos pelo usuário atual

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

A última linha é incorreta, mas você pode ver onde eu estava tentando ir com ele. Eu sou muito novo para LINQ, então eu não muito pensar sobre isso da maneira certa ainda. Onde estou indo errado?

Foi útil?

Solução

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))
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top