Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove item from list using linq

Tags:

c#

linq

How to remove item from list using linq ?.

I have a list of items and each item it self having a list of other items now I want to chaeck if other items contains any items of passed list so main item should be remove. Please check code for more clarity.

public Class BaseItems
{
    public int ID { get; set; }
    public List<IAppointment> Appointmerts { get; set; }
}

Public DeleteApp(List<IAppointment> appointmentsToCheck)
{
   List<BaseItems> _lstBase ; // is having list of appointments

   //now I want to remove all items from _lstBase  which _lstBase.Appointmerts contains 
   any item of appointmentsToCheck (appointmentsToCheck item and BaseItems.Appointmerts 
   item is having a same reference)

   //_lstBase.RemoveAll(a => a.Appointmerts.Contains( //any item from appointmentsToCheck));

}
like image 966
Neeraj Kumar Gupta Avatar asked Oct 19 '12 13:10

Neeraj Kumar Gupta


People also ask

How to remove particular item from List based on condition in c#?

C# | Remove all elements of a List that match the conditions defined by the predicate. List<T>. RemoveAll(Predicate<T>) Method is used to remove all the elements that match the conditions defined by the specified predicate.

How do I delete a record in LINQ query?

You can delete rows in a database by removing the corresponding LINQ to SQL objects from their table-related collection. LINQ to SQL translates your changes to the appropriate SQL DELETE commands. LINQ to SQL does not support or recognize cascade-delete operations.


1 Answers

_lstBase
    .RemoveAll(a => a.Appointmerts.Any(item => appointmentsToCheck.Contains(item)));
like image 137
Jan P. Avatar answered Sep 20 '22 20:09

Jan P.