0

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();
0

1 Answer 1

2

If I'm reading you right, you're trying to return the original set of items with the "external" relations filtered out.

Your existing query will essentially just return the original list without any filtering, if it works at all.

Instead, try selecting a new set of items:

var items = (from i in catalog.Items
             select new Item
             {
                 Id = i.Id,
                 Relation = i.Relation == null ? null : i.Relation.Where(r => r.Type != "external").ToArray(),
             }).ToArray();
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Matt, your statement will work only if all item has relation, but some item don't have relation, so this will throw an error "Value cannot be null." but i get the idea. thanks :)
I've updated the answer (untested) to do a null check for you, @Cornelis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.