使用linq从列表中删除项目
问题描述:
如何使用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));
}
答
_lstBase
.RemoveAll(a => a.Appointmerts.Any(item => appointmentsToCheck.Contains(item)));