从ID通用列表中删除对象 [英] Remove object from generic list by id
本文介绍了从ID通用列表中删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个领域类是这样的:
I have a domain class like this:
public class DomainClass
{
public virtual string name{get;set;}
public virtual IList<Note> Notes{get;set;}
}
我怎么会去从删除项目在的IList<注意>
?我能做到这一点,如果它是一个清单,但它必须是一个的IList
,因为我使用NHibernate我的持久化层。
How would I go about removing an item from the IList<Note>
? I would be able to do it if it was a List but it has to be an IList
as I am using Nhibernate for my persistance layer.
在理想情况下,我想这样的方法在我的域类:
Ideally I wanted a method like this in my domain class:
public virtual void RemoveNote(int id)
{
//remove the note from the list here
List<Note> notes = (List<Note>)Notes
notes.RemoveAll(delegate (Note note)
{
return (note.Id = id)
});
}
但我不能投的的IList
为列表
。有没有更优雅的方式这一轮?
But I can't cast the IList
as a List
. Is there a more elegant way round this?
推荐答案
您可以过滤掉不想要的物品,并创建一个新的列表只有项目你想:
You could filter out the items you don't want and create a new list with only the items you do want:
public virtual void RemoveNote(int id)
{
//remove the note from the list here
Notes = Notes.Where(note => note.Id != id).ToList();
}
这篇关于从ID通用列表中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文