2014-03-29 156 views
1

我有一個關於我正在做的小項目的問題。我目前有一個列表框填充了類型爲「遊戲」的對象。在遊戲中,有名稱,GameConsole,流派等屬性...c# - 在列表框中過濾一個對象的屬性

我想根據這些屬性過濾我的結果。我想知道這是否可能。我通過Access數據庫中的DA類獲取我的遊戲對象。

我從DA我場比賽,這段代碼:

listBoxGames.BeginUpdate(); 
DAGame daGame = new DAGame(); 
List<Game> games = daGame.GetGames(); 

foreach (Game game in games) 
{ 
    listBoxGames.Items.Add(game); 
} 
listBoxGames.EndUpdate(); 

編輯:這是我的遊戲對象的聲明

public Game(int id, string naam, string afbeelding, float score, string opmerkingen, bool online, bool coOp, int maxAantalSpelers, int multiplayerTypeId, int systeemId, int genreId, int jaar, bool touchControls, string uitgeleend, bool favoriet) 
     { 
      this.Id = id; 
      this.Naam = naam; 
      this.Afbeelding = afbeelding; 
      this.Score = score; 
      this.Opmerkingen = opmerkingen; 
      this.Online = online; 
      this.CoOp = coOp; 
      this.MaxAantalSpelers = maxAantalSpelers; 
      this.MultiplayerTypeId = multiplayerTypeId; 
      this.SysteemId = systeemId; 
      this.GenreId = genreId; 
      this.Jaar = jaar; 
      this.TouchControls = touchControls; 
      this.Uitgeleend = uitgeleend; 
      this.Favoriet = favoriet; 
} 

在此先感謝

+0

你可以添加聲明你的遊戲對象?目前尚不清楚您的意思是[屬性](http://msdn.microsoft.com/zh-cn/library/aa288454(v = vs.71).aspx)或屬性。但你一定在尋找[LINQ](http://msdn.microsoft.com/en-us/library/bb397933.aspx)。網絡上有很多例子。 – Vasek

回答

0

這可能是你想要什麼

var nameFilterList=games.Where(x=>x.Name="SomeName").ToList(); 
+0

這看起來好像有點我可以使用的代碼,但是可以幫助我使用變量嗎? :) 我真的不知道如何開始實施它。 –

+0

實際上,'使用變量嗎?' –

+0

我真的不知道在什麼情況下我應該使用此代碼。我應該如何處理nameFilterList變量以及遊戲變量中的內容? –

1

這可以幫助我想。

listBoxGames.BeginUpdate(); 
    DAGame daGame = new DAGame(); 
    List<Game> games = daGame.GetGames(); 

    foreach (Game game in games) 
    { 
     listBoxGames.Items.Add(game.Where(x => x.Name.Equals("ThisName").Single()); 
    } 
    listBoxGames.EndUpdate(); 
相關問題