I want to create a list out of an xml file where only values between two letters are displayed. For example, only show matching records where the first character of the title are in the range from “A” to “E”.
My first basic approach was
where item.Element("title").Value >= "A" && item.Element("title").Value < "E"
Of course this didn’t work as these operators can’t be applied to strings.
I tried using string.compare but without luck.
Can anyone give me an example on how to achieve this query?
cheers,
Terry
Update: Below is the code that for reading the xml.
var simpleListSource = (from item in doc.Descendants("item")
where item.Element("title").Value[0] >= 'A' && item.Element("title").Value[0] < 'E'
orderby item.Element("title").Value
select new
{
title = item.Element("title").Value,
description = item.Element("description").Value,
image = item.Element("image").Value,
type = item.Element("type").Value,
imagelink = item.Element("imagelink").Value
}).ToList();
rptGetItems.DataSource = simpleListSource;
rptGetItems.DataBind();
And the xml…
<item>
<title>A test</title>
<titlelong>A</titlelong>
<type>test</type>
<description>
Some description
</description>
<image>includes/images/test.jpg</image>
<imagelink>http://somesite.com</imagelink>
</item>
<item>
<title>E test2</title>
<titlelong>E</titlelong>
<type>test</type>
<description>
Another sample
</description>
<image>includes/images/test.jpg</image>
<imagelink>http://somesite.com</imagelink>
</item>
Use chars and compare them to the first char in the title:
You might want to test for Empty titles…
(update)
Your code works as expected. Just change the
< Eto<= E. Try printing the titles to console:Your problem must be related to data binding.