考虑到下面的情况,我需要获取属于分配给顶层的中间对象列表的所有底层对象。我的结构意味着中层对象挂在顶层,但底层只是所有对象的集合。因此,即使列表不存在,我也需要获取所有有效“挂起”顶层和中间层的底层对象。在 lambda 中有一个很好的方法来做到这一点吗?
class Program
{
static void Main(string[] args)
{
List<TopClass> topClasses = new List<TopClass>();
List<MiddleClass> mcList1 = new List<MiddleClass>();
mcList1.Add(new MiddleClass(1));
mcList1.Add(new MiddleClass(3));
mcList1.Add(new MiddleClass(5));
TopClass tc1 = new TopClass(1, mcList1);
topClasses.Add(tc1);
List<MiddleClass> mcList2 = new List<MiddleClass>();
mcList2.Add(new MiddleClass(2));
mcList2.Add(new MiddleClass(4));
TopClass tc2 = new TopClass(2, mcList2);
topClasses.Add(tc2);
List<MiddleClass> mcList3 = new List<MiddleClass>();
mcList3.Add(new MiddleClass(6));
mcList3.Add(new MiddleClass(7));
TopClass tc3 = new TopClass(3, mcList3);
// ** Note not added to the global list
List<BottomClass> bottomClasses = new List<BottomClass>();
bottomClasses.Add(new BottomClass(10, 1));
bottomClasses.Add(new BottomClass(20, 2));
bottomClasses.Add(new BottomClass(30, 3));
bottomClasses.Add(new BottomClass(40, 4));
bottomClasses.Add(new BottomClass(50, 5));
bottomClasses.Add(new BottomClass(60, 6));
bottomClasses.Add(new BottomClass(70, 7));
bottomClass.FindAll(b => ....?
}
}
public class TopClass //Instruments
{
public int Id { get; set; }
public List<MiddleClass> MiddleClasses { get; set; }
public TopClass(int id, List<MiddleClass> middleClasses)
{
this.Id = id;
this.MiddleClasses = middleClasses;
}
}
public class MiddleClass // Sequences
{
public int Id { get; set; }
public MiddleClass(int id)
{
this.Id = id;
}
}
public class BottomClass //SequenceItem
{
public int Id { get; set; }
public int MiddleClassId { get; set; }
public BottomClass(int id, int middleClassId)
{
this.Id = id;
this.MiddleClassId = middleClassId;
}
}
结果将是一个包含 10 到 50 但不包含 60 和 70 的列表。