所以我有一个对象的列表,让我们称它们为大象。
每个大象对象都有一个名为ClonedFrom的属性。这是大象的类型,用于指向这个新对象在其图像中创建的对象。
Elephant类还有一个名为HasTrunk的属性,它的类型为bool。
所以:
public class Elephant
{
public Elephant ClonedFrom { get; set; }
public bool HasTrunk { get; set; }
}我们有
List<Elephant> herd我希望有一个LINQ查询,该查询将返回任何具有假HasTrunk属性的象群,但它的ClonedFrom属性值也等于同一列表中的另一个大象,该列表的HasTrunk属性设置为true。
例如,克隆大象A来创造新的大象B和C
B和C都存在于羊群列表中,两者具有相同的ClonedFrom属性值(A)。B将HasTrunk设置为false,而C将hasTrunk设置为true。
我想要一个返回B的查询。
(注:A是否在名单中并不重要)
发布于 2014-10-04 15:05:36
List<Elephant> herd = new List<Elephant>{
new Elephant(), new Elephant(), new Elephant(), new Elephant()
};
herd[0].HasTrunk = true;
herd[1].HasTrunk = true;
herd[2].HasTrunk = false;
herd[3].HasTrunk = false;
herd[0].ClonedFrom = herd[0];
herd[1].ClonedFrom = new Elephant();
herd[2].ClonedFrom = herd[0];
herd[3].ClonedFrom = new Elephant();
herd.Where(elephant => !elephant.HasTrunk && herd.Where(elephant2 =>
elephant2.HasTrunk).Any(elephant2 => elephant2.ClonedFrom == elephant.ClonedFrom)); //One item - elephant number 2https://stackoverflow.com/questions/26193955
复制相似问题