我在int []中想要检查列表中的特定属性是否存在于数组中.这是我的财产,
public class WaitingLists { [Key] public Int32 Id { get; set; } public Guid UserId { get; set; } public Int32 GaMetableId { get; set; } public Int32 WaitingListTypeId { get; set; } **public Int32 ? StakeBuyInId { get; set; }** }
然后我想检查我的列表中是否存在StakeBuyInId.
这是Linq的代码,
public GameListItem[] GetMyWaitingList(Guid UserId,int[] WaitingListTypeIds,int[] StakeBuyInIds) { ProviderDB db = new ProviderDB(); List<GameListItem> objtempGameListItem = new List<GameListItem>(); List<GaMetables> objGaMetablesList = new List<GaMetables>(); var objWaitingListUser = db.WaitingLists.Where(x => x.UserId.Equals(UserId)); if (WaitingListTypeIds != null) { objWaitingListUser = objWaitingListUser.Where(x => WaitingListTypeIds.Contains(x.WaitingListTypeId)); } **if (StakeBuyInIds != null) { objWaitingListUser = objWaitingListUser.Where(x => x.StakeBuyInId != null ? StakeBuyInIds.Contains(x.StakeBuyInId) : false); }** return objtempGameListItem.ToArray(); }
但是它显示了一个错误,Contains不允许’int? ”.它只会重载’int’.所以你有任何想法如何使用linq使用Contains for null属性?谢谢你的帮助.
解决方法
尝试
StakeBuyInIds.Contains((Int32)x.StakeBuyInId)
要么
objWaitingListUser = objWaitingListUser.Where(x => x.StakeBuyInId.HasValue && StakeBuyInIds.Contains((Int32)x.StakeBuyInId));