131

Вопрос по list, c# – Как удалить элемент из списка в C #?

Error: User Rate Limit Exceeded

var resultlist = results.ToList();

Error: User Rate Limit Exceeded

ID FirstName  LastName
-- ---------  --------
1  Bill       Smith
2  John       Wilson
3  Doug       Berg

Error: User Rate Limit Exceeded

7ответов

3

Error: User Rate Limit Exceeded

resultList.Remove(resultList.Single(x => x.Id == 2));

Error: User Rate Limit Exceeded

35

resultList = results.Where(x=>x.Id != 2).ToList();

Error: User Rate Limit Exceeded

public static IEnumerable<T> ExceptWhere<T>(this IEnumerable<T> source, Predicate<T> predicate)
{
    return source.Where(x=>!predicate(x));
}

//usage in above situation
resultList = results.ExceptWhere(x=>x.Id == 2).ToList();
3

Error: User Rate Limit ExceededRemoveAt(index)Error: User Rate Limit ExceededRemove(obj)Error: User Rate Limit Exceeded

// Remove(obj)
var item = resultList.Single(x => x.Id == 2);
resultList.Remove(item);

// RemoveAt(index)
resultList.RemoveAt(1);
4

Error: User Rate Limit ExceededList.FindIndexError: User Rate Limit ExceededList.RemoveAt.

Error: User Rate Limit ExceededprobablyError: User Rate Limit ExceededWhere/ToListError: User Rate Limit ExceededmutatesError: User Rate Limit Exceeded

Error: User Rate Limit ExceededFindIndexError: User Rate Limit ExceededRemoveAtError: User Rate Limit ExceededRemoveAtError: User Rate Limit ExceededRemoveError: User Rate Limit ExceededsecondError: User Rate Limit Exceeded

Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded

var list = new List<int> { 1, 3, 2 };
var index = list.FindIndex(i => i == 2); // like Where/Single
if (index >= 0) {   // ensure item found
    list.RemoveAt(index);
}
list.Dump();        // results -> 1, 3

Error: User Rate Limit Exceeded

0

Error: User Rate Limit Exceededresultlist.RemoveAt(1)Error: User Rate Limit Exceeded

263

List<T>Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededError: User Rate Limit Exceeded

resultlist.RemoveAt(1);

Error: User Rate Limit ExceededError: User Rate Limit Exceeded:

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

Error: User Rate Limit ExceededError: User Rate Limit Exceeded. SingleOrDefaultError: User Rate Limit ExceedednullError: User Rate Limit ExceededSingleError: User Rate Limit Exceededid).

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

Short answer:
Remove (from list results)

results.RemoveAll(r => r.ID == 2);Error: User Rate Limit ExceededID 2Error: User Rate Limit ExceededresultsError: User Rate Limit Exceeded

Filter (without removing from original list results):

var filtered = result.Where(f => f.ID != 2);Error: User Rate Limit ExceededID 2

Detailed answer:

Error: User Rate Limit Exceeded.RemoveAll()Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

class myClass {
    public int ID; public string FirstName; public string LastName;
}

Error: User Rate Limit ExceededresultsError: User Rate Limit Exceeded

var results=new List<myClass> {
    new myClass()  { ID=1, FirstName="Bill", LastName="Smith" },
    new myClass()  { ID=2, FirstName="John", LastName="Wilson" },
    new myClass()  { ID=3, FirstName="Doug", LastName="Berg" },
    new myClass()  { ID=4, FirstName="Bill", LastName="Wilson" },
};

Error: User Rate Limit Exceeded

var removeList = new List<int>() { 2, 3 };

Error: User Rate Limit Exceeded

results.RemoveAll(r => removeList.Any(a => a==r.ID));

Error: User Rate Limit Exceededremove the items 2 and 3Error: User Rate Limit ExceededremoveList. NoteError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

results.RemoveAll(r => r.ID==4);

Error: User Rate Limit Exceeded


Error: User Rate Limit ExceededError: User Rate Limit Exceeded

RelatedQuestions