我的应用程序现在有一个非常简单的模型,我正在尝试找到遍历聚合的最佳方法。正如您在底部的模型图中看到的那样,我有一个帐户、一次旅行和参加旅行的人员列表。我希望能够查看一个帐户所属的所有旅行,我想出了这样的东西:
public List<Trip> GetAllTripsFor(int accountid)
{
var tripRepository = new TripRepository();
var trips = tripRepository.FindAll();
var userTrips = new List<Trip>();
foreach (Trip trip in trips)
{
foreach (TripPeople person in trip.People)
{
// If this trip's person list contains this accountid
// then add this trip to the userTrips list.
if(person.Account.Id == accountid)
userTrips.Add(trip);
}
}
return userTrips;
}
对我来说,这似乎不是很有效,而且我没有正确地思考事情。你们中的任何人对实现这一点的更好方法有什么想法吗?也许我在想我的模型错了?谢谢