来自多个ListBox项的LINQ to XML查询

时间:2011-06-26 20:21:34

标签: asp.net linq-to-xml

我需要一个linq到xml查询的例子。

我有两个ListBox,SelectionMode设置为Multiple

我填充第一个ListBox的查询是:

var query = doc.Root.Descendants("Articles")
                    .OrderBy(b => b.Element("Category").Value)
                    .Select(b => b.Element("Category").Value)
                    .Distinct();

并将其与:

绑定
lbxItems.DataSource = query;
lbxItems.DataBind();

所以我在第一个ListBox中拥有所有值,当我从该ListBox中选择项目时,我想填充第二个ListBox。

所以在SelectedIndexChanged我有另一个问题:

var query = doc.Root.Descendants("Articles")
                .Where(b => b.Element("Category").Value.Equals(lbxItems.SelectedValue))
                    .OrderBy(b => b.Element("SubCategory").Value)
                    .Select(b => b.Element("SubCategory").Value)
                    .Distinct();

如果我选择一个项目,那是有效的,但是我需要一个查询,它可以从多个选定的项目中执行相同的操作。

谢谢。

1 个答案:

答案 0 :(得分:2)

尝试更改where子句,如下所示:

.Where(b => lbxItems.Items
    .Cast<ListItem>() // needs a cast
    .Where(i => i.Selected)
    .Select(i => i.Value)
    .Contains(b.Element("Category").Value))

我们的想法是确定选择了哪些项目,并查看您的类别值是否属于所选项目。