Following is my class definition
[XmlRoot("catalog")]
public class Catalog
{
[XmlElement("item")]
public Item[] item{ get; set; }
}
[XmlType("item")]
public class Item
{
[XmlElement("id")]
public string id { get; set; }
[XmlElement("relation", typeof(Relation))]
public Relation[] relation { get; set; }
}
[Serializable]
public class Relation
{
[XmlAttribute("weight")]
public string weight { get; set; }
[XmlText]
public string Value { get; set; }
[XmlElement("id")]
public string id { get; set; }
[XmlElement("type")]
public string type { get; set; }
[XmlElement("name")]
public string name { get; set; }
}
Here are sample data
<catalog>
<item>
<id>18338517</id>
<relation weight="100">
<type>External</type>
<id>123</id>
<name>Mcday</name>
</relation>
<relation weight="99">
<type>Internal</type>
<id>234</id>
<name>Mcnight</name>
</relation>
</item>
<item>
<id>18339999</id>
</item>
<item>...</item>
</catalog>
I want to get all item, but remove relation inside item that fill certain criteria. e.g.: relation.type = "external" so my desired output would be:
<catalog>
<item>
<id>18338517</id>
<relation weight="99">
<type>Internal</type>
<id>234</id>
<name>Mcnight</name>
</relation>
</item>
<item>
<id>18339999</id>
</item>
<item>...</item>
</catalog>
I try following linq statement with no success
var selected = from data in catalog.Term
from relation in data.Relation
where relation.Type != "external"
select data;
Term[] temp = selected.ToArray<Term>();
Thanks in advance.
Edit Based on Matt's reply the statement should be
var items = (from i in catalog.Items
select new Item
{
Id = i.Id,
Relation = i.Relation != null ? i.Relation.Where(r => r.Type != "external").ToArray() : null,
}).ToArray();