Remove object from generic list by id

前端 未结 6 1424
你的背包
你的背包 2021-02-07 10:50

I have a domain class like this:

public class DomainClass
{
  public virtual string name{get;set;}
  public virtual IList Notes{get;set;}
}
         


        
6条回答
  •  梦如初夏
    2021-02-07 11:49

    If you can change the datastructure I would suggest using a Dictionary. Than you can go with:

    public class DomainClass
    {
      public virtual string name{get;set;}
      public virtual IDictionary Notes {get; set;}
    
      //Helper property to get the notes in the dictionary
      public IEnumerable AllNotes
      {
        get
        {
          return notes.Select (n => n.Value);
        }
      }
    
      public virtual void RemoveNote(int id)
      {
         Notes.Remove(id);
      }
    

    }

    If ID is not unique use IDictionary> instead.

提交回复
热议问题