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)
假设您有一个
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
}
}
}