I have in int[] and want to check that particular property from list exist into an array or not. Here is my class of property,
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 a 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 is showing me an error that Contains does not allow 'int ? '. It only overloads 'int'. So do you have any idea how to use Contains for null property using linq? Thanks for any help.
In C# programs, a List reference can be null. This is not the same as it being empty and having zero elements.
The Nullable type allows you to assign a null value to a variable. Nullable types introduced in C#2.0 can only work with Value Type, not with Reference Type. The nullable types for Reference Type is introduced later in C# 8.0 in 2019 so that we can explicitly define if a reference type can or can not hold a null value.
You typically use a nullable value type when you need to represent the undefined value of an underlying value type. For example, a Boolean, or bool , variable can only be either true or false . However, in some applications a variable value can be undefined or missing.
try
StakeBuyInIds.Contains((Int32)x.StakeBuyInId)
OR
objWaitingListUser = objWaitingListUser.Where(x => 
                 x.StakeBuyInId.HasValue  && 
                 StakeBuyInIds.Contains((Int32)x.StakeBuyInId));
                        you could 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 query would looks like this
objWaitingListUser = objWaitingListUser.Where(x => StakeBuyInIds.Contains(x.StakeBuyInId))
instead of
objWaitingListUser = objWaitingListUser.Where(x => x.StakeBuyInId != null 
                                                   ? StakeBuyInIds.Contains(x.StakeBuyInId) 
                                                   : false);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With