Robert
Robert

Reputation: 1726

Linq results based on a property of an object in a inner list

I have the following xml that I'm trying to get some results from:

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <Roles>
    <Role name="File Server">
      <Image></Image>
      <AppToRun name="VM">
        <AppOption name="KVM" value="FS-CentOS67" />
      </AppToRun>
    </Role>
    <Role name="VCS Gateway">
      <Image></Image>
      <AppToRun name="VCS_Server" dependentPC="FS" />
      <AppToRun name="SpeechGenerator" dependentPC="FS" />
    </Role>
    <Role name="Supervisor">
      <Image>LTInstructor.png</Image>
    </Role>
    <Role name="Ground Controller">
      <Image>Final.png</Image>
      <DependentRoles>
        <DependentRole name="File Server" />
        <DependentRole name="VCS Gateway" />
      </DependentRoles>
      <AppToRun name="VM" dependentPC="FS">
        <AppOption name="KVM" value="Tower1-CentOS67" />
      </AppToRun>
      <AppToRun name="VCS">
        <AppOption value="VCS-LC" />
        <AppOption value="VCS-GC" />
      </AppToRun>
      <AppToRun name="LXV" />
        <AppOption value="start_speech_controller_LCGC.bat" />
      </AppToRun>
    </Role>
    <Role name="Pilot 1">
      <Image>Pilot.jpg</Image>
      <DependentRoles>
        <DependentRole name="File Server" />
        <DependentRole name="VCS Gateway" />
        <DependentRole name="Ground Controller" />
      </DependentRoles>
      <AppToRun name="VM" dependentPC="FS">
        <!-- FS is defined in HOSTS file -->
        <AppOption name="KVM" value="Pilot1-CentOS67" />
        <AppOption name="KVM" value="Pilot2-CentOS67" />
      </AppToRun>
      <AppToRun name="VCS">
        <AppOption value="VCS-PP1" />
      </AppToRun>
    </Role>
  </Roles>
</root>

Each "Role" is essentially a computer. Roles can have 0 or many Dependent Roles. If a Role has a Dependent Role, then it cannot start until the Dependent Role is started first. I've got all that working but now I'm trying to determine is when someone stops a computer, are there any Roles that depend on it? If so, don't stop the pc.

Here are my objects: Role

public class Role
{
    public string RoleName { get; set; }
    public string ImageName { get; set; }
    public List<DependentRole> DependentRoles { get; set; }
    public List<AppToRun> AppsToRun { get; set; }

    public Role()
    {
        if (AppsToRun == null)
            AppsToRun = new List<AppToRun>();

        if (DependentRoles == null)
            DependentRoles = new List<DependentRole>();
    }
}

AppToRun

public class AppToRun
{
    public string Name { get; set; }
    public List<AppOption> AppOptions { get; set; }
    public bool AppProcessed { get; set; } = false;
    public string DependentPC { get; set; }
    public AppToRun()
    {
        if (AppOptions == null)
            AppOptions = new List<AppOption>();
    }
}

AppOption

public class AppOption
{
    public string Name { get; set; }
    public string Value { get; set; }
    public string IPAddress { get; set; }
}

DependentRole

public class DependentRole
{
    public string Name { get; set; }
}

I tried something like this but it comes back with the error `Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'bool'.

IEnumerable<DependentRole> r2 = Roles.Where(r => r.DependentRoles.Where(dr => dr.Name == roleName);

How can I return a list of DependentRoles who's Name property is a specified value (in this case, "Pilot 1")?

Upvotes: 0

Views: 132

Answers (1)

Danil Eroshenko
Danil Eroshenko

Reputation: 490

This should work:

IEnumerable<DependentRole> r2 = Roles.SelectMany(r => r.DependentRoles.Where(dr => dr.Name == roleName));

See MSDN for details

Upvotes: 1

Related Questions