我有以下情況:在我的資料庫表中,Schedule我有列:
IsConfirmed, IsAnonymous, DateTimeUtcRequestSend, EmailAddress, UMCN, Deleted, ServiceId
用戶可以是注冊用戶或匿名用戶。如果用戶是匿名的,它有一個標志,IsAnonymous = 1否則標志是 0。如果用戶是匿名的,他必須點擊鏈接,他有 10 分鐘的激活時間。欄位DateTimeUtcRequestSend是他創建請求的時間。
如果用戶已注冊,則自動IsConfirmed設定為1。對于注冊用戶DateTimeUtcRequestSend沒有任何目的。這兩種型別的用戶都必須輸入電子郵件(必填欄位),如果他們愿意,可以輸入 umcn(非強制性)。
每個用戶注冊與否可以檢查多個服務(serviceId FKs串列)
我需要創建一個查詢來檢查是否有任何行包含以下用戶:
- 注冊用戶 (IsConfirm = 1) 并且具有相同的 UMCN或電子郵件并且至少有 1 個相同的 serviceId
- 匿名用戶(IsConfirm = 0 和 IsAnonymous = 1 和
DateTime.UtcNow - DateTimeUtcRequestSend < 10min或IsConfirm = 1 和 IsAnonymous = 1)上述條件,如果是,則回傳一條訊息,新插入是不可能的。
我試過這種方法:
var query = scheduleService.GetAll()
.Where(x => !x.Deleted
&& x.EmailAddress == data.email || x.UMCN== data.UMCN
&& x.IsConfirmed
&& servicesIdDistinctedBy.Contains(x.ServiceId));
但我不知道如何在用戶未被確認并且他是匿名的并且在 10 分鐘的時間范圍內添加相反的條件:
var query = scheduleService.GetAll()
.Where(x => !x.Deleted
&& x.EmailAddress == data.email || x.UMCN== data.UMCN
&& x.IsConfirmed
&& (!x.IsConfirmed && x.IsAnonymous.Value && DateTime.UtcNow - x.DateTimeUtcRequestSend < Constants.ConfirmationTimeWindow)
&& servicesIdDistinctedBy.Contains(x.ServiceId));
這個括號會幫助“隔離”條件嗎?也許我需要使用.Any()function 而不是.Where()?
我是否需要一些Expression<Func<T>>才能根據某些條件進行區分?我覺得沒必要?
uj5u.com熱心網友回復:
首先,您應該避免使用GetAll(),它會掃描您的整個表,而不僅僅是查詢您需要的內容,并且隨著表的增長需要越來越長的時間。相反,你應該去:
var query = scheduleService.Where(x =>...
或者
var query = scheduleService.Any(x =>...
這個括號會幫助“隔離”條件嗎?
是的,它會,但你必須小心你如何使用它,因為它可以改變你的狀況的整體含義。
也許我需要使用
.Any()函式而不是.Where()
如果你的目標只是為了確認是否有在你的表中的一行對應于你的條件,那么,你應該使用Any(),因為它會顯著比快Where(),因為它會阻止通過表檢索,一旦發現它看起來行為了。
另一方面,如果您需要獲取符合您條件的所有資料,那么您需要堅持使用Where().
從您的解釋來看,我并不完全清楚您需要什么,但據我所知,我認為以下內容應該會有所幫助:
var query = scheduleService.Where(x => !x.Deleted
&& (x.EmailAddress == data.email || x.UMCN== data.UMCN)
&& ((x.IsConfirmed || (!x.IsConfirmed && x.IsAnonymous.Value))
&& (DateTime.UtcNow - x.DateTimeUtcRequestSend < Constants.ConfirmationTimeWindow)))
&& servicesIdDistinctedBy.Contains(x.ServiceId));
uj5u.com熱心網友回復:
如果我理解正確,您是在問已確認的用戶是否可以不受時間限制地檢查某些服務,或者匿名用戶是否可以在 10 分鐘內完成。
這歸結為操作順序以及將您的需求正確解釋為代碼以進行有效查詢。讓我們分解一下。我將嘗試使用您的變數名稱。
你有這個條件
注冊用戶 (IsConfirm = 1) 并且具有相同的 UMCN 或電子郵件并且至少有 1 個相同的 serviceId
就其本身而言,結果如下。括號很重要,可以消除對操作順序的懷疑。
// registered user
x.IsConfirmed
// whose email address matches or whose UMCN matches
&& (x.EmailAddress == data.email || x.UMCN== data.UMCN)
// and whose service ID is one of the set of distinct service IDs
&& servicesIdDistinctedBy.Contains(x.ServiceId)
然后你有
匿名用戶(IsConfirm = 0 and IsAnonymous = 1 and DateTime.UtcNow - DateTimeUtcRequestSend < 10min or IsConfirm = 1 and IsAnonymous = 1)上述條件,如果是,則回傳一條訊息,新插入是不可能的。
這涉及以下內容(在您的文本中,您說“兩種型別的用戶都必須輸入電子郵件(必填欄位),并且如果他們愿意,可以輸入 umcn(非強制性)。”所以我重新包括此條件:
// anonymous user is both not confirmed
!x.IsConfirmed
// and must have a value that is true (a case where == true is justified)
// i.e. we can't just do x.IsAnonymous because that could throw as it is nullable
&& x.IsAnonymous?.Value == true
// whose email address matches or whose UMCN matches
&& (x.EmailAddress == data.email || x.UMCN== data.UMCN)
&& DateTime.UtcNow - x.DateTimeUtcRequestSend < Constants.ConfirmationTimeWindow
&& servicesIdDistinctedBy.Contains(x.ServiceId)
然后必須對這兩個條件進行OR 'd 和簡化.. OR 'ing 我們得到
(
x.IsConfirmed
&& (x.EmailAddress == data.email || x.UMCN== data.UMCN)
&& servicesIdDistinctedBy.Contains(x.ServiceId)
)
||
(!x.IsConfirmed
&& x.IsAnonymous?.Value == true
&& (x.EmailAddress == data.email || x.UMCN== data.UMCN)
&& DateTime.UtcNow - x.DateTimeUtcRequestSend < Constants.ConfirmationTimeWindow
&& servicesIdDistinctedBy.Contains(x.ServiceId)
)
在簡化之后(使用我們可以重新排列AND并分解出公共部分的事實),我們得到
// match either mandatory email or optional UMCN
(x.EmailAddress == data.email || x.UMCN == data.UMCN)
&& (
// and must be confirmed
x.IsConfirmed
// or anonymous, completing in 10 minutes
|| (!x.IsConfirmed
&& x.IsAnonymous?.Value == true
&& DateTime.UtcNow - x.DateTimeUtcRequestSend < Constants.ConfirmationTimeWindow))
// and finally with the distinct service ID
&& servicesIdDistinctedBy.Contains(x.ServiceId)
這就是條件。現在,您需要將其轉化為查詢。您的查詢應該使用Anynot Where。您希望查看用戶是否滿足單個需求,而不是很多需求,這樣Any就足夠了,但是Where太多了。
If GetAll() returns IQueryable you can add the Any() clause after GetAll() returns and still defer the entire thing so it runs on the server against the DB rather than after getting everything and then applying the condition in memory.
If that's not the case, it would be better to append the Any() to the enumerable directly.
scheduleService.AnyMatch(x => ...)
vs.
scheduleService.GetAll().Where(x => ...)
As you can see, this whole compound condition can be a single lambda and doesn't require expressions to get into the mix.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/347480.html
標籤:C# asp.net 。网 asp.net-mvc 林克
上一篇:Nav和Div出現的距離太遠了?
下一篇:從資料庫獲取List()
