LINQ : 테이블과 관련 테이블의 조건
-
06-09-2019 - |
문제
나는 몇 가지 항목이 있습니다 티커 생성 및 구독 할 수 있습니다. 그들이 만들어지면 1 : 개인 또는 2 : 공개 범위가 있습니다. 공개 티커는 누구나 구독 할 수 있으며 소유자 초대에 의해 개인 티커를 구독 할 수 있습니다.
LINQ를 사용하여 [1]이 현재 사용자가 구독 한 사용자 (예 : 특정 소유자 ID)가 만든 티커를 얻고 싶습니다.
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));
}
마지막 줄은 잘못되었지만 내가 어디로 가려고했는지 알 수 있습니다. 나는 LINQ를 처음 접했기 때문에 아직 올바른 방법으로 생각하지 않습니다. 내가 어디로 잘못 가고 있습니까?
해결책
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))
제휴하지 않습니다 StackOverflow