Robert
Robert

Reputation: 4406

How to use linq to find a sub element in a list / IEnumerable?

I have an XML structure that I parse and return as an IEnumerable:

 <menuItem Id="1">
      <menuItem Id="2">
            <menuItem Id="5">
                  <menuItem Id="9">
                       <criterion></criterion>
                  </menuItem>
             </menuItem>
       </menuItem>
  </menuItem>
  <menuItem Id="10>
    /// huge xml document from here on...

I parse it using this class:

 public static  class XmlConverter
{

    public static IEnumerable<MenuItem> Parser()
    {
        using (var fileStream = new FileStream(HttpContext.Current.Server.MapPath("~/DataSource/MenuNavigation.xml"), FileMode.Open))
        {
            return Read(fileStream);
        }
    }

    private static IEnumerable<MenuItem> Read(Stream stream)
    {
        return (IEnumerable<MenuItem>)XmlSerializer().Deserialize(stream); ;
    }


    private static XmlSerializer XmlSerializer()
    {
        return new XmlSerializer(typeof(List<MenuItem>), GetXmlRootAttribute());

    }

    private static XmlRootAttribute GetXmlRootAttribute()
    {
        return new XmlRootAttribute() { ElementName = "Navigation", IsNullable = true };
    }
}

It is parsed into this object:

  public class MenuItem
{
    [XmlAttribute("Id")]
    public int Id { get; set; }
    [XmlAttribute("Name")]
    public string Name { get; set; }
    [XmlAttribute("Description")]
    public string Description { get; set; }
    [XmlAttribute("Link")]
    public string Link { get; set; }
    [XmlAttribute("Role")]
    public string RoleId { get; set; }
    [XmlAttribute("Alert")]
    public string Alert { get; set; }
    [XmlElement("Criterion")]
    public List<Criterion> Criteria { get; set; }

    private List<MenuItem> _menuItems = new List<MenuItem>();

    [XmlElement("MenuItem")]
    public MenuItem[] MenuItems
    {
        get { return _menuItems.ToArray(); }
        set
        {
            _menuItems.Clear();
            if (value != null)
            {
                _menuItems.AddRange(value);
            }
        }
    }
}

When I do a Linq query on the IEnumerable that I get back I use:

 var criteria = menuItems.Where(x => x.Id == menuItem.Id)
                         .Select(x => x.Criteria)
                         .FirstOrDefault();

The problem is the IEnumerable here only has 14 items. Those of the parent Items so if I am looking for Id == 9 then it is not found because it is a sub element of Id 1.

How do I edit the linq statement above so that it checks each level with an element named menuItem and not just the parents for the Id in question?

Update:

The IEnumerable looks like this:

  Count=14
 +MenuItem //nested menuItem
  Name

Upvotes: 0

Views: 1144

Answers (1)

NSGaga
NSGaga

Reputation: 14302

You could flatten the hierarchy first (linq inherently doesn't have any support for that) - and then do what you're doing.

e.g. How do I select recursive nested entities using LINQ to Entity

...and then

var item = menuItems.Flatten(y => y.MenuItems).Where(x => x.Id == 5)
    .FirstOrDefault();

Upvotes: 1

Related Questions