2013-06-14 14 views
6

Mam int [] i chcę sprawdzić, czy konkretna właściwość z listy istnieje w tablicy, czy nie. Oto moja klasa własności,Sprawdza Zawiera tablicę int dla właściwości null

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

Następnie chcę sprawdzić, czy StakeBuyInId istnieje na mojej liście.

Oto kod 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(); 
     } 

Ale to pokazuje mi błąd, który zawiera nie pozwala „int? ". To tylko przeciąża "int". Więc masz pojęcia, jak używać Contains dla właściwości null przy użyciu linq? Dzięki za pomoc.

Odpowiedz

5

spróbować

StakeBuyInIds.Contains((Int32)x.StakeBuyInId) 

LUB

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

lub lepszych 'StakeBuyInIds.Contains (x.StakeBuyInId.GetValueOrDefault())' lub 'objWaitingListUser = objWaitingListUser.Where (X => StakeBuyInIds.Contains (x.StakeBuyInId.GetValueOrDefault()); ' –

0

można także utworzyć rozszerzenie

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

     return false; 
    } 

i zapytanie będzie wygląda to

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

zamiast

objWaitingListUser = objWaitingListUser.Where(x => x.StakeBuyInId != null 
                ? StakeBuyInIds.Contains(x.StakeBuyInId) 
                : false); 
Powiązane problemy