本文介绍了System.NotSupportedException:“集合为只读".从iList中删除对象时抛出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
运行下面的代码片段时会引发异常.
I'm getting an exception thrown when running the snippet of code below.
我有一个iListof Webelements,如果该元素包含字符串"WSC",我想将其从iList中删除.
I have an iListof webelements, and if that element contains the string "WSC", I'd like to remove it from the iList.
有人可以帮我吗?下面的代码.
Can anyone help me out? Code below.
IList<IWebElement> tableRows;
tableRows = FindElement(By.XPath("//div[@ui-grid='vm.gridWoodSmokeViolations']")).FindElements(By.XPath("//div[@role='row']"));
Console.WriteLine("table row count before loop: " + tableRows.Count);
foreach (var row in tableRows) {
if (row.Text.ToString().Contains("WSC"))
{
tableRows.Remove(row); //exception thrown here
}
}
感谢您可能提供的帮助
推荐答案
并非所有实施IList的东西都是可编辑的.最简单的解决方案是使用Linq来构建过滤器并对其执行ToList().
Not all things that implments IList are editable. The easiest solution is just use Linq to build a filter and do a ToList() on it.
IList<IWebElement> tableRows;
tableRows = FindElement(By.XPath("//div[@ui-grid='vm.gridWoodSmokeViolations']")).FindElements(By.XPath("//div[@role='row']"));
Console.WriteLine("table row count before loop: " + tableRows.Count);
tableRows = tableRows.Where(row=> !row.Text.ToString().Contains("WSC")).ToList();
这篇关于System.NotSupportedException:“集合为只读".从iList中删除对象时抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!