我又来了……我有一个 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>
在 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();