views:

93

answers:

1

I have some items called tickers which can be created and subscribed to. When they are created, they have a scope of 1:private or 2:public. Public tickers can be subscribed to by anyone and private tickers can be subscribed to by owner invitation.

I want to use LINQ to get the tickers created by a user (ie: with a certain owner id) which either [1] are public [2] have been subscribed to by the current user.

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

That last line is incorrect but you can see where I was trying to go with it. I'm pretty new to LINQ so I don't quite think of it the right way yet. Where am I going wrong?

+2  A: 
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))
Daniel Brückner
This is nice but isn't restricted by owner id. I guess I can just replace db.Tickers with GetTickersByOwnerAll(ownerId)
Dinah