如何访问超过一种自定义类型深度的属性?

问题描述 投票:0回答:1

C# 初学者在这里。我有这个:

public class Item
{
   public int id { get; set; }
   public string name { get; set; }
   public virtual ICollection<Property> Properties { get; set; } 
{

public class Property
{
   public int PropertyId { get; set; }
   public string Owner { get; set; }
}

例如,如何访问条件中嵌套的所有者字符串?这就是我想要的:

foreach (var item in ListItem)
{
    if (item.Properties.Owner == "abc")
    { do something }
}

我试过了,没有效果:

item.Properties.Select(s => s.Owner)
c# .net lambda
1个回答
0
投票

假设您有一个

Item
元素列表 - 那么
.Properties
属性又是 Property 对象的
list
- 所以您需要执行以下操作:

foreach (Item item in ListItem)
{
    // enumerate over the properties
    foreach (Property prop in item.Properties)
    {
        // once you're at the level of a *single* "Property" - then you can
        // get at the owner of that property 
        if (prop.Owner == "abc")
        { 
            // do something 
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.