【发布时间】:2009-02-11 09:00:23
【问题描述】:
又是我…… 我有一个 XML 文件,其中包含我想查询不同属性的不同类别。
<item>
<title>Industries</title>
<category type="Channel">Automotive</category>
<category type="Type">Cars</category>
<category type="Token">Article</category>
<category type="SpecialToken">News</category>
<guid>637f0dd7-57a0-4001-8272-f0fba60feba1</guid>
</item>
IN SQL 我会写类似的东西。
select * from articles where channel = 'Automative' AND type = 'Cars' etc. etc.
如何使用 linq 实现这一点? 我尝试了以下查询,但它返回 null。如果我将这两个属性与“或”||运算符我会得到结果,但如果一个项目同时符合两个条件,则会得到所有双重结果。
var articleList = (from item in doc.Descendants("item")
from _category in item.Elements("category")
where _category.Value == valueCboChannel && _category.Attribute("domain").Value == "Channel"
&& (_category.Value == valueCboSubChannel && _category.Attribute("domain").Value == "SubChannel")
select new
{
Title = item.Element("title").Value,
Guid= item.Element("guid").Value,
description = item.Element("description").Value,
link = item.Element("link").Value
}).ToList();
ListView1.DataSource = articleList;
ListView1.DataBind();
【问题讨论】:
标签: c# linq linq-to-xml