0
我有2個表: 'AllowedDates'如何配置LINQ要求
- DayID int PK
- Day datetime
'AllowedTimes'
- TimeID int PK
- DayID int FK
- Hour int
- Minute int
我也有表 '用戶':
- ID int PK
- FirstName nvarchar(max)
...
和表'UserDeniedTimes':
DayID int FK
UserID int FK
Hour int
Minute int
,我需要選擇用戶,這沒有否認時間(UserDeniedTimes記錄)混凝土大衛·/小時/分鐘
我儘量做到以下幾點:
var result = from i in _dbContext.Users
where i.UserDeniedTimes.All(
p => (!p.AllowedDate.AllowedTimes.Any(
a1 => a1.DayID == aTime.DayID
&& a1.Hour == aTime.Hour
&& a1.Minute == aTime.Minute
))
)
select new ...
它工作正常,但有一個例外。如果用戶有一天在UserDeniedTimes中記錄,但又有一次,則此用戶也不會被選中。例如,UserDeniedTimes有記載:
DayID = 10
UserID = 20
Hour = 14
Minute = 30
該用戶將沒有選擇,如果有atime的以下值:
DayID = 10
Hour = 9
Minute = 30
但會選擇是否=大衛·11爲什麼呢?
[加] 它正常工作時,我只限制在白天:
var result = from i in _dbContext.Users
where i.UserDeniedTimes.All(
p => (!p.AllowedDate.AllowedTimes.Any(
a1 => a1.DayID == aTime.DayID
))
)
select new ...
但不工作的時候我寫:
var result = from i in _dbContext.Users
where i.UserDeniedTimes.All(
p => (!p.AllowedDate.AllowedTimes.Any(
a1 => a1.Hour == 14
))
)
select new ...
爲什麼呢? DayID和Hour有什麼區別?
[增加了#2]
((time == null) || i.UserDeniedTimes.All(p =>
//p.AllowedDate.AllowedTimes.Any(a1 => a1.DayID != 33) &&
(p.AllowedDate.AllowedTimes.Any(a2 => a2.Hour != 14)
))) &&
不起作用
((time == null) || i.UserDeniedTimes.All(p =>
p.AllowedDate.AllowedTimes.Any(a1 => a1.DayID != 33) &&
//(p.AllowedDate.AllowedTimes.Any(a2 => a2.Hour != 14)
))) &&
工作
爲什麼呢?
DayID = 11,UserID = 20,Hour = 8, Minute = 30可能有另一條記錄。 –
DayID = 11,UserID = 20,Hour = 8,Minute = 30沒有記錄。只有當DayID不存在時,此用戶記錄纔會被選中 – user994038
是什麼問題? – user994038