Linq结果基于内部列表中对象的属性

时间:2017-09-06 13:04:09

标签: c# linq linq-to-xml

我有以下xml,我正试图从中获得一些结果:

<?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>

每个“角色”本质上都是一台计算机。角色可以有0个或多个从属角色。如果角色具有从属角色,则在首先启动从属角色之前,它无法启动。我已经完成了所有工作,但现在我想确定当有人停止计算机时,是否有任何依赖它的角色?如果是这样,请不要停止电脑。

以下是我的对象: 的作用

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; }
}

我尝试了类似这样的东西,但它返回错误“无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'bool'。

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

如何返回Name属性为指定值的DependentRoles列表(在本例中为“Pilot 1”)?

1 个答案:

答案 0 :(得分:1)

这应该有效:

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

有关详细信息,请参阅MSDN