XML LINQ: How to use select with where?
I have an XML catalog with product Name and product Price.
I successfully select all items from my catalog with this code:
var products = xElem.Descendants("catalog")
.Select(x => new
{
开发者_C百科 ProductPrice= x.Element("Price").Value,
ProductName = x.Element("Name").Value
});
How do I modify the above code to select only items whose names contain "abc" and "xyz"? In SQL I'd use WHERE and LIKE, here, I want to do this with LINQ. Thanks.
MORE INFO
Actually, I want the user to be able to query flexibly. So if the user types "red shirt cotton" then I will show all items that has all those 3 terms: "red", "shirt" and "cotton". The number of "where" terms are not fixed.
var products = from d in xElem.Descendants("catalog")
where d.Element("Name").Value.Contains("abc")
|| d.Element("Name").Value.Contains("xyz")
select new { Name = d.Element("Name") .Value,
Price = d.Element("Price").Value
}
;
精彩评论