當 db 列長為 Nullable 時,我們應該做什么,這里 (Visit) 并且我有要比較的 VisitorIds?
long[] VisitorIds = new long[] {1,2,3}; //an array to check with
.Where(x => vivitorIds.Contains(x.Visit)).ToList();
如果我在 linq 中轉換為 long,則會拋出例外。
.Where(x => vivitorIds.Contains(Convert.ToInt64(x.Visit))).ToList();
uj5u.com熱心網友回復:
只需檢查 nullable 是否有值:
.Where(x => x.Visit.HasValue && vivitorIds.Contains(x.Visit.Value)).ToList();
uj5u.com熱心網友回復:
我們可以使用null conditional operatorwith null coalescing,
//Considering long.MinValue is not a part of vivitorIds list.
.Where(x => vivitorIds.Contains(x?.Visit?.Value ?? long.MinValue)).ToList();
注意:這只是一種替代方法,Tim 的解決方案比這個解決方案更干凈
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/425680.html
