獲取添加我有一個名爲估計類,它具有以下字段和屬性:元素不在列表中
private IList<RouteInformation> _routeMatrix;
public virtual IList<RouteInformation> RouteMatrix
{
get
{
if (_routeMatrix != null && _routeMatrix.Count > 0)
{
var routeMatrix = _routeMatrix.ToList();
routeMatrix =
routeMatrix.OrderBy(tm => tm.Level.LevelType).ThenBy(tm => tm.Level.LevelValue).ToList();
return routeMatrix;
}
else return _routeMatrix;
}
set { _routeMatrix = value; }
}
因此,在getter方法,我只是排序的等級類型,然後_routeMatrix通過級別值並返回排序列表。
以我方案之一,我有以下代碼:
public void SaveApprovers(string[] approvers)
{
int i = 1;
foreach (var approver in approvers)
{
var role = Repository.Get<Role>(long.Parse(approver));
var level = new Models.Level
{
LevelType = LevelType.Approver,
LevelValue = (LevelValue)i,
Role = role
};
Repository.Save(level);
var routeInformation = new Models.RouteInformation
{
Level = level,
RouteObjectType = RouteObjectType.Estimate,
RouteObjectId = _estimate.Id
};
Repository.Save(routeInformation);
_estimate.RouteMatrix.Add(routeInformation); // <--- The problem is here
Repository.Save(_estimate);
i++;
}
}
的問題是,如果有多個批准(即:approvers
陣列的長度大於1,只有第一routeInformation
被添加到RouteMatrix
我不知道其他人會發生什麼,但Add方法不會給出任何錯誤
此前,RouteMatrix是一個公共字段。它是私人的,並將其封裝在一個公共財產。
什麼是var routeMatrix = _routeMatrix.ToList();對於? var routeMatrix = _routeMatrix.OrderBy(tm => tm.Level.LevelType).ThenBy(tm => tm.Level.LevelValue).ToList(); –