Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Contains into an int array for null property

Tags:

c#

linq

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.

like image 425
KomalJariwala Avatar asked Jun 14 '13 06:06

KomalJariwala


People also ask

Can list have NULL values C#?

In C# programs, a List reference can be null. This is not the same as it being empty and having zero elements.

How to use nullable types in c#?

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.

What is nullable bool?

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.


2 Answers

try

StakeBuyInIds.Contains((Int32)x.StakeBuyInId)

OR

objWaitingListUser = objWaitingListUser.Where(x => 
                 x.StakeBuyInId.HasValue  && 
                 StakeBuyInIds.Contains((Int32)x.StakeBuyInId));
like image 114
Damith Avatar answered Oct 15 '22 16:10

Damith


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);
like image 27
WiiMaxx Avatar answered Oct 15 '22 15:10

WiiMaxx