我有一个像这样的域类:
public class DomainClass
{
public virtual string name{get;set;}
public virtual IList<Note> Notes{get;set;}
}
我将如何从IList<Note>
?如果它是一个列表我就能做到,但它必须是一个IList
因为我使用 Nhibernate 作为我的持久层。
理想情况下,我想要在我的域类中使用这样的方法:
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
as a List
。有没有更优雅的方法来解决这个问题?
您可以过滤掉您不想要的项目,并创建一个仅包含您想要的项目的新列表:
public virtual void RemoveNote(int id)
{
//remove the note from the list here
Notes = Notes.Where(note => note.Id != id).ToList();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)