sab669 sab669 - 1 year ago 122
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

from my
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>();
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)

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 Source

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"));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download