How to Remove Item from List in C#

How to remove item from list in C#?

List<T> has two methods you can use.

RemoveAt(int index) can be used if you know the index of the item. For example:

resultlist.RemoveAt(1);

Or you can use Remove(T item):

var itemToRemove = resultlist.Single(r => r.Id == 2);
resultList.Remove(itemToRemove);

When you are not sure the item really exists you can use SingleOrDefault. SingleOrDefault will return null if there is no item (Single will throw an exception when it can't find the item). Both will throw when there is a duplicate value (two items with the same id).

var itemToRemove = resultlist.SingleOrDefault(r => r.Id == 2);
if (itemToRemove != null)
resultList.Remove(itemToRemove);

How to: Remove an item from a Liststring

You have to get the index of the object you wanna remove from the list, then you can:

//Assuming companies is a list 
companies.RemoveAt(i);

To get the index of the item you can use :

companies.IndexOf("Item");

or use a for loop with conditional statements:

for (int i = 0; i < companies.Count; i++) {
// if it is List<String>
if (companies[i].equals("Something")) {
companies.RemoveAt(i);
}
}

How to remove elements from a generic list while iterating over it?

Iterate your list in reverse with a for loop:

for (int i = safePendingList.Count - 1; i >= 0; i--)
{
// some code
// safePendingList.RemoveAt(i);
}

Example:

var list = new List<int>(Enumerable.Range(1, 10));
for (int i = list.Count - 1; i >= 0; i--)
{
if (list[i] > 5)
list.RemoveAt(i);
}
list.ForEach(i => Console.WriteLine(i));

Alternately, you can use the RemoveAll method with a predicate to test against:

safePendingList.RemoveAll(item => item.Value == someValue);

Here's a simplified example to demonstrate:

var list = new List<int>(Enumerable.Range(1, 10));
Console.WriteLine("Before:");
list.ForEach(i => Console.WriteLine(i));
list.RemoveAll(i => i > 5);
Console.WriteLine("After:");
list.ForEach(i => Console.WriteLine(i));

How to remove an item in a list that equals a specific string

try list.RemoveAll(x=>x=="start it");
where list is your List<string>

How to Quickly Remove Items From a List

List isn't an efficient data structure when it comes to removal. You would do better to use a double linked list (LinkedList) as removal simply requires reference updates in the adjacent entries.

How to Remove elements from one List based another list's element and condition?

You can remove the elements like this:

list1.RemoveAll(item => list2.Any(item2 => item.Key == item2.Key))

Remove item from list which doesnt exist in other

There is no need to load all the data into memory doing List<Data> dbData = dbcontext.Datas.ToList();.

You could load only the entities to delete:

var myDataIds = myData.Select(a => a.Id).ToList();
var dataToDelete = db.Datas.Where(a => myDataIds.Contains(a.Id)).ToList();
db.Datas.RemoveRange(dataToDelete);
db.SaveChanges();

It's still not ideal that you need to load the data just to delete it. In this case though, you would only load 1 entity, instead of 3.

Are you familiar with Dapper? It's a micro ORM that would allow you to do this just by writing a sql query. In the projects that I work, we use both EntityFramework and Dapper, and choose based on our needs.



Related Topics



Leave a reply



Submit