Check Contains int in array for null property

I have int [] and want to check if a particular property from the list exists in the array or not. Here is my property class,

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; }** } 

Then I want to check that StakeBuyInId exists in my list.

Here is the code for 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(); } 

But it shows me an error that contains does not allow "int?". It overloads only "int". So you know how to use Contains for a null property using linq? Thanks for any help.

+6
source share
2 answers

try

 StakeBuyInIds.Contains((Int32)x.StakeBuyInId) 

OR

 objWaitingListUser = objWaitingListUser.Where(x => x.StakeBuyInId.HasValue && StakeBuyInIds.Contains((Int32)x.StakeBuyInId)); 
+5
source

you can also create an extension

  public static bool Contains<T>(this IList<T> container, T? content) { if (content.HasValue) if (container.Contains(content.Value)) return true; return false; } 

and your request will look like this:

 objWaitingListUser = objWaitingListUser.Where(x => StakeBuyInIds.Contains(x.StakeBuyInId)) 

instead

 objWaitingListUser = objWaitingListUser.Where(x => x.StakeBuyInId != null ? StakeBuyInIds.Contains(x.StakeBuyInId) : false); 
0
source

Source: https://habr.com/ru/post/947276/


All Articles