在行程中,您可以定义起点和目的地,还可以添加航点。
添加航点时,如果已有1个或多个航点,用户可以选择要放置的位置,如果不选择位置,则为最后一个,编辑航点时他还可以改变他的位置,当删除任何航点时,列表必须重新组织。
挑战在于如何正确管理列表中的航点,以便在流程结束时我将列表与有组织的航点一起保存。我从未对列表进行过复杂的操作。
public class Itinerary : BaseEntity, IAggregateRoot
{
public Itinerary(int id, Address start, Address end, List<Waypoint> waypoints = null)
{
Id = id;
Start = start;
End = end;
_waypoints = waypoints;
}
public Itinerary()
{
}
public Address Start { get; set; }
public Address End { get; set; }
private readonly List<Waypoint> _waypoints = new List<Waypoint>();
public IReadOnlyCollection<Waypoint> Waypoints => _waypoints.AsReadOnly().OrderBy(x => x.Position).ToList();
public List<int> AvailableWaypoints()
{
List<int> waypoints = new List<int>();
for (int i = 0; i < _waypoints.Count(); i++)
{
waypoints.Add(_waypoints[i].Position);
}
return waypoints.OrderBy(x => x).ToList();
}
public void AddWaypoint(Waypoint waypoint)
{
//...
}
public void UpdateWaypoint(Waypoint waypoint)
{
//...
}
public void RemoveWaypoint(Waypoint waypoint)
{
//...
}
}
public class Waypoint : BaseEntity
{
public Waypoint(int id, Address address, int position, int itineraryId, Itinerary itinerary = null)
{
Id = id;
Address = address;
Position = position;
ItineraryId = itineraryId;
Itinerary = itinerary;
}
public Waypoint()
{
}
public Address Address { get; set; }
public int Position { get; set; }
public int ItineraryId { get; set; }
public Itinerary Itinerary { get; set; }
}
答案 0 :(得分:0)
在指定的索引处插入一个元素到列表中。 List.Insert
有了这个,也许您可以在航点位置将航点插入列表中。请记住,您可能需要处理以下事实:索引从 0 开始,而您的航路点位置可能从 1 开始。
_waypoints.Insert(waypoint.Position - 1, waypoint);