我在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; }**
}
Run Code Online (Sandbox Code Playgroud)
然后我想检查我的列表中是否存在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();
}
Run Code Online (Sandbox Code Playgroud)
但它显示我包含不允许'int?的错误?".它只会重载'int'.所以你有任何想法如何使用linq使用Contains for null属性?谢谢你的帮助.
尝试
StakeBuyInIds.Contains((Int32)x.StakeBuyInId)
Run Code Online (Sandbox Code Playgroud)
要么
objWaitingListUser = objWaitingListUser.Where(x =>
x.StakeBuyInId.HasValue &&
StakeBuyInIds.Contains((Int32)x.StakeBuyInId));
Run Code Online (Sandbox Code Playgroud)