Filtering XML data with LinQ
I'm developing a Windows Phone 7 a开发者_高级运维pp.
I have a list of Ids from a table and I want to filter a XML file that has all information about these table.
I have the following C# code:
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),
. . .
};
If I have a List<long> unfinishedGamesId
. I want to filer results getting every game that hasn't got an Id from unfinishedGamesId. Something like:
c.Attribute("game_id").Value != unfinishedGamesId[0] &&
c.Attribute("game_id").Value != unfinishedGamesId[1] &&
...
How can I add this to where clause?
What are you trying to do? If you're wanting to include data whose values are in that list, the query would be:
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),
. . .
};
Assuming the Id value is the one you're getting with uint.Parse
, this code should work:
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),
. . .
};
Doing the attribute lookup and parse twice is not very efficient, but I'm not sure how to fix that.
精彩评论