使用 LinQ 过滤 XML 数据

发布于 2024-11-02 07:14:19 字数 738 浏览 0 评论 0原文

我正在开发一个 Windows Phone 7 应用程序。

我有一个表中的 ID 列表,我想过滤一个包含这些表的所有信息的 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;未完成的GamesId。我想归档结果,获取每个未从 unfinishedGamesId 获得 Id 的游戏。比如:

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

我怎样才能将其添加到 where 子句中?

I'm developing a Windows Phone 7 app.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人生戏 2024-11-09 07:14:19

你想做什么?如果您想要包含其值在该列表中的数据,则查询将是:

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),
       . . .
   };

:属性查找和解析两次不是很有效,但我不知道如何解决这个问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文