Linq 结果基于内部列表中对象的 属性
Linq results based on a property of an object in a inner list
我试图从以下 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>
每个"Role"本质上都是一台电脑。角色可以有 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);
如何 return 一个 DependentRoles 列表,其中 Name
属性 是指定值(在本例中,"Pilot 1")?
这应该有效:
IEnumerable<DependentRole> r2 = Roles.SelectMany(r => r.DependentRoles.Where(dr => dr.Name == roleName));
详情见MSDN
我试图从以下 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>
每个"Role"本质上都是一台电脑。角色可以有 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);
如何 return 一个 DependentRoles 列表,其中 Name
属性 是指定值(在本例中,"Pilot 1")?
这应该有效:
IEnumerable<DependentRole> r2 = Roles.SelectMany(r => r.DependentRoles.Where(dr => dr.Name == roleName));
详情见MSDN