2011-04-19 71 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