使用lambda表達式根據條件刪除內部列表元素。我已經通過foreach達成了。需要使用lambda表達式或LINQ使用lambda表達式根據條件刪除內部列表元素
實現請幫
if (!FwContext.User.UserRoles.Exists(s => s.RoleCd == "Admin"))
{
foreach (var item in items.ToList())
{
foreach (var attribute in item.ItemAttributeValues.ToList())
{
if (attribute.AttributeNm == "EST" && attribute.StatusNm == "SAP")
{
item.ItemAttributeValues.Remove(attribute);
}
}
}
}
在遍歷它不能修改的集合。 – dcg
這是家庭嗎?如果您需要使用lambda表達式或linq來完成,爲什麼要使用foreach循環? –
如果我們使用.ToList(),我們可以修改一個集合,並且上面的代碼工作正常,沒有任何錯誤。 – NishanthMV