does not contain a definition for RemoveAll and no extension method 'RemoveAll' Accepting [Resolved]

Posted by Kasani007 under LINQ on 5/2/2016 | Points: 10 | Views : 8574 | Status : [Member] | Replies : 3
'System.Collections.Generic.IList<Address>' does not contain a definition for 'RemoveAll' and no extension method 'RemoveAll' accepting a first argument of type 'System.Collections.Generic.IList<Address>' could be found (are you missing a using directive or an assembly reference?)


why the error is coming...and what to do to rectify it..?


thanks in advance




Responses

Posted by: Sheonarayan on: 5/2/2016 [Administrator] HonoraryPlatinum | Points: 50

Up
0
Down

Resolved
The reason is quite simple, IList collection object doesn't contain the RemoveAll method. To make this work change the definition to List like
System.Collections.Generic.List<Address> addresses = new System.Collections.Generic.List<Address>();
addresses.RemoveAll(...);

and this should work.

Hope this helps.

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 5/3/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
@Kasani007, I completely agree with Sheo Sir...However, you can simulate the situation as under

        public static void RemoveAll(IList<Addresses> list)
{
while(list.Count !=0)
{
list.RemoveAt(0);
}
}


and use like

 static void Main(string[] args)
{
IList<Addresses> addr = new List<Addresses>();
//Loading dummy values
Addresses objAddress = new Addresses();
objAddress.Id = 1;
objAddress.Address = "Test Address1";
addr.Add(objAddress);

Addresses objAddress2 = new Addresses();
objAddress2.Id = 2;
objAddress2.Address = "Test Address2";
addr.Add(objAddress2);

Addresses objAddress3 = new Addresses();
objAddress3.Id = 3;
objAddress3.Address = "Test Address3";
addr.Add(objAddress3);

RemoveAll(addr);

}


where

public class Addresses
{
public long Id { get; set; }
public string Address { get; set; }
}


--
Thanks & Regards,
RNA Team

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 5/3/2016 [Member] [MVP] Silver | Points: 25

Up
0
Down
IList have a method called clear to remove all items.

You can use that like given below

IList<Addresses> addr = new List<Addresses>();
//Adding your recrods
addr.Clear();


Complete code
 IList<Addresses> addr = new List<Addresses>();
//Loading dummy values
Addresses objAddress = new Addresses();
objAddress.Id = 1;
objAddress.Address = "Test Address1";
addr.Add(objAddress);

Addresses objAddress2 = new Addresses();
objAddress2.Id = 2;
objAddress2.Address = "Test Address2";
addr.Add(objAddress2);

Addresses objAddress3 = new Addresses();
objAddress3.Id = 3;
objAddress3.Address = "Test Address3";
addr.Add(objAddress3);
addr.Clear();


You can check this link for more details
https://msdn.microsoft.com/en-us/library/system.collections.ilist.clear(v=vs.110).aspx

Thanks,
A2H
My Blog

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response