【发布时间】:2023-03-25 00:43:02
【问题描述】:
假设我有一个继承自同一个基类的对象列表。那么是否可以通过 LINQ 获取仅在其中一个子类中指定的值?在我的示例中,我想找到具有特定对象且具有特定属性的实例?
我在 Linqpad 中做了这个例子:
void Main()
{
var list = new List<A>
{
new B
{
MyProp = new D{ OtherProp = 1}
},
new C(),
new B
{
MyProp = new D{ OtherProp = 30}
},
};
list.Where(x => ....) // how to find the instance where OtherProp == 30 ?
}
public class A
{
public int JustAprop { get; set; }
}
public class B : A
{
public D MyProp { get; set; }
}
public class C : A
{
}
public class D
{
public int OtherProp { get; set; }
}
【问题讨论】:
-
这对我来说似乎是个错误的设计
-
你能详细说明一下@rahul 吗?