我有一个这样的类结构:
class MyClass
{
public IEnumerable<AttributeGroup> AttributeGroups { get; set; }
}
class AttributeGroup
{
public IEnumerable<Attribute> Attributes { get; set; }
}
class Attribute
{
public string SomeProp { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我需要获得具有特定"SomeProp"值的所有"属性",无论它们属于哪个属性组.
例如,SomeProperty== 'A'可以在两个被发现MyClassObj.AttributeGroup[0],并MyClassObj.AttributeGroup[5]和我需要写一个LINQ(或类似的东西),以这两种不同attributegroups取两个对象.
有什么建议吗?
Jus*_*now 23
首先从所有属性组中选择所有属性,然后仅选择具有属性的属性.
IEnumerable<Attribute> attributes =
myClassInstance
.AttributeGroups
.SelectMany(x => x.Attributes)
.Where(x => x.SomeProperty == 'A');
Run Code Online (Sandbox Code Playgroud)
其他Linq风格的语法:
IEnumerable<Attribute> attributes =
from attributeGroup in myClassInstance.AttributeGroups
from attribute in attributeGroup.Attributes
where attribute.SomeProperty == 'A'
select attribute;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29245 次 |
| 最近记录: |