sab669 sab669 - 1 month ago 14
C# Question

LINQ Invalid arguments for Collections.Generic.List.RemoveAll

I have this method which is telling me I have some invalid arguments where I'm trying to call

RemoveAll
from my
List<SelectListItem>
object. I simply just need to remove a few items from a dropdown list based upon a simple condition.

public JsonResult GetExportTables(CaseListDynExport objCaseListDynExport)
{
List<SelectListItem> lstExportTablesList = new List<SelectListItem>();
try
{
CaseListDynExportBLL objCaseListDynExportBLL = new CaseListDynExportBLL();

DataTable dtExportTables = objCaseListDynExportBLL.GetExportTables(objCaseListDynExport);

lstExportTablesList = DropDownHelper.GetSelectListItem("TableName", "TableExportCode", null, dtExportTables);

if (objCaseListDynExport.someCondition)
lstExportTablesList.RemoveAll(lstExportTablesList.Where(l => l.Text.IndexOf("Audit") >= 0));
}
catch (Exception ex)
{
LogUtility.ErrorException(ex);
}

return Json(lstExportTablesList, JsonRequestBehavior.AllowGet);
}


I must be missing something really obvious, but I can't spot what I'm doing wrong.

I've looked at these posts:



But yea; can't spot what I'm doing incorrectly...

Answer

It should be:

lstExportTablesList.RemoveAll(l => l.Text.IndexOf("Audit") >= 0);

So you pass a lambda method directly to the RemoveAll method for checking the condition.

A neater way, with the same functionality, would be:

lstExportTablesList.RemoveAll(l => l.Text.Contains("Audit"));
Comments