score:76
using linq:
prods.remove( prods.single( s => s.id == 1 ) );
maybe you even want to use singleordefault()
and check if the element exists at all ...
edit:
since stuff
is a struct, singleordefault()
will not return null. but it will return default( stuff ), which will have an id of 0. when you don't have an id of 0 for your normal stuff-objects you can query for this id:
var stufftoremove = prods.singleordefault( s => s.id == 1 );
if( stufftoremove.id != 0 )
{
prods.remove( stufftoremove );
}
score:-2
you could use linq.
var prod = from p in prods
where p.id != 1
select p;
score:0
you can only remove something you have a reference to. so you will have to search the entire list:
stuff r;
foreach(stuff s in prods) {
if(s.id == 1) {
r = s;
break;
}
}
prods.remove(r);
or
for(int i = 0; i < prods.length; i++) {
if(prods[i].id == 1) {
prods.removeat(i);
break;
}
}
score:0
prods.remove(prods.single(p=>p.id == 1));
you can't modify collection in foreach, as vincent suggests
score:1
here is a solution for those, who want to remove it from the database with entity framework:
prods.removewhere(s => s.id == 1);
and the extension method itself:
using system;
using system.linq;
using system.linq.expressions;
using microsoft.entityframeworkcore;
namespace livanova.ngpdm.client.services.data.extensions
{
public static class dbsetextensions
{
public static void removewhere<tentity>(this dbset<tentity> entities, expression<func<tentity, bool>> predicate) where tentity : class
{
var records = entities
.where(predicate)
.tolist();
if (records.count > 0)
entities.removerange(records);
}
}
}
p.s. this simulates the method removeall()
that's not available for db sets of the entity framework.
score:3
prods.remove(prods.find(x => x.id == 1));
score:8
if you have linq:
var itemtoremove = prods.where(item => item.id == 1).first();
prods.remove(itemtoremove)
score:238
if your collection type is a list<stuff>
, then the best approach is probably the following:
prods.removeall(s => s.id == 1)
this only does one pass (iteration) over the list, so should be more efficient than other methods.
if your type is more generically an icollection<t>
, it might help to write a short extension method if you care about performance. if not, then you'd probably get away with using linq (calling where
or single
).
Source: stackoverflow.com
Related Query
- Remove item from list based on condition
- Remove Duplicate item from list based on condition
- Remove list from a list in C# based on specific condition
- Remove duplicate items from list if certain properties match and get the top item based on some ordering
- How to get next item from list based on some condition in C#?
- Remove item from list using linq
- Remove duplicates from list based on multiple fields or columns
- Remove a specific item from a list using LINQ
- Remove All from List where each line doesn't contain any item from another list
- Selecting items from generic list based on true condition from another list
- How to Remove elements from one List based another list's element and condition?
- Removing certain elements from list condition based on another list
- use LINQ to remove items from a list based on a date
- Create array of property values from list based on condition
- How can i copy data table records of different field name based on mapping list evaluating condition on source data table?
- Find an item from a list based on an ID
- How to remove item from an anonymous list
- Remove an object from a list based on an attribute of another list with linq
- Remove older file(s) from list based on create date
- Remove items from list based on another, w.r.t. positions
- LINQ to remove objects from a list of objects based on another list of objects
- How to select multiple item from list with related condition
- Remove an Item from C# Enum List
- Get list based on condition from another generic list
- USe LINQ to remove from a list based on conditions
- how to use Except or Remove method based on nested list condition
- Remove item from List C#
- How to select an entity from database based on nested list item match?
- C# Want to remove elements from IEnumerable<Dictionary<string, object>> based on condition using lambda expression?
- Remove item from xml list with Linq
More Query from same tag
- Ho to Convert a Uint to Nullable Int in a EF Query
- How do I determine whether an IEnumerable of roles contains any roles from string array?
- How to write predicate for union join and inner queries?
- LINQ OrderBy different field type depending on IF statement
- Is LINQ to SQL the best way to build a Model or create my own classes
- Best exception handling approaches in Linq:
- LINQ - "SQL statement is nested too deeply" issue in MVC application
- ASP.net Core C# Linq Distinct not working
- C# Create xml from Scratch
- How can I query against a concatenated field?
- adding Xelement according to specific condition c#
- C# Extract list of fields from list of class
- Populating a List(Of String) from a Datatable using LINQ in VB.NET
- Insert data using LINQ with stored procedures while using two projects in c#
- LINQ Projected Filtering C#
- Conditionally sorting elements by multiple properties in multiple tables with LINQ
- How can I make my procedure for finding the Nth most frequent element in an array more efficient and compact?
- Concatenate two list of different type
- LINQ: Get all objects with a matching string property from an incoming IEnumerable<string> in a performant way
- How to return a list of strings using LINQ
- Display List of Objects in a group with self referencing foreign key
- Delete a sql row using Linq in C#
- Custom order by, is it possible?
- LINQ to XML - Populate/Update the XElement with data from a DataTable
- Convert charArray to byteArray
- Creating Nested Groupings using Linq Extension Methods
- Given IComparer, Zip two ordered IEnumerable-s, pairing only equal elements?
- Using LINQ Group Joins in VB.NET
- Adding additional linq where clauses based on variables
- Retrieve a list B from a list A