2011-04-19 74 views
0

我正在開發Windows Phone 7應用程序。用LinQ過濾XML數據

我有一個表中的Ids列表,我想過濾一個XML文件,它包含有關這些表的所有信息。

我有下面的C#代碼:

var filteredData = 
    from c in loadedData.Descendants("gameDescription") 
    where c.Attribute("gameType").Value == gameType && 
      c.Attribute("language").Value.Equals(language) 
    select new SampleData.GamesDesc() 
    { 
     Id = uint.Parse(c.Attribute("game_id").Value), 
     . . . 
    }; 

如果我有一個List<long> unfinishedGamesId。我想讓每個沒有從unfinishedGamesId獲得Id的遊戲都得到Filer結果。例如:

c.Attribute("game_id").Value != unfinishedGamesId[0] && 
c.Attribute("game_id").Value != unfinishedGamesId[1] && 
... 

如何將此添加到where子句?

回答

0

你想做什麼?如果你想包括數據,其值是在該列表中,查詢將是:

var filteredData = 
    from c in loadedData.Descendants("gameDescription") 
    where c.Attribute("gameType").Value == gameType && 
      c.Attribute("language").Value.Equals(language) && 
      unfinishedGamesId.Contains(c.Id) 
    select new SampleData.GamesDesc() 
    { 
     Id = uint.Parse(c.Attribute("game_id").Value), 
     . . . 
    }; 

假設將ID值你與uint.Parse得到一個,這個代碼應工作:

var filteredData = 
    from c in loadedData.Descendants("gameDescription") 
    where c.Attribute("gameType").Value == gameType && 
     c.Attribute("language").Value.Equals(language) && 
     unfinishedGamesId.Contains(uint.Parse(c.Attribute("game_id").Value)) 
    select new SampleData.GamesDesc() 
    { 
     Id = uint.Parse(c.Attribute("game_id").Value), 
     . . . 
    }; 

做屬性查找和解析兩次不是很有效,但我不知道如何解決這個問題。

+0

我用更好的細節更新了我的問題。 – VansFannel 2011-04-19 14:42:38

+0

難題...因爲你想在列表中沒有條目的值,使用「!(unfinishedGamesId.Contains(...))」應該做的伎倆。 – NateTheGreat 2011-04-19 14:45:05