M
mp
is there a good way to "index" custom objects in a list for removal?
similar to SortedDictionary.Remove(key)
how to do that with List<Criteria>.Remove( ? )
i found two ways so far, comments?
from http://stackoverflow.com/questions/2742313/list-remove-question
1)
....said to be using Link ?(of which i know exactly nothing)
Customer customer = collCustList.Where(c => c.Number == 99 && c.Type == "H"
/* etc */).FirstOrDefault();
if (customer != null)
{
collCustList.Remove(customer);
}
....or ....
2)
collCustList.RemoveAll(c => c.Number == 99 && c.Type == "H" /* etc */);
i'm using the .Where version and it seems to work.
//this fires when user checks an item in listview
private void lvCriteriaList_ItemCheck(object sender,
System.Windows.Forms.ItemCheckEventArgs e)
{
string key=this.lvCriteriaList.Items[e.Index].SubItems[1].Text;
string value=this.lvCriteriaList.Items[e.Index].Text;
if (_criteriaChoiceList == null)
{
_criteriaChoiceList = new List<Criteria>();
}
if (e.NewValue == CheckState.Checked)
{
AddCriteria(key, value);
}
else if (e.NewValue == CheckState.Unchecked)
{
RemoveCriteria(key, value);
}
DisplayCriteria();
}
private void AddCriteria(string key, string value )
{
_criteriaChoiceList.Add(new Criteria(key, value));
}
private void RemoveCriteria(string key, string value)
{
Criteria criteria1 = _criteriaChoiceList.Where(c => c.Key ==
key && c.Value == value ).FirstOrDefault();
if (criteria1 != null)
{
_criteriaChoiceList.Remove(criteria1);
}
}
thanks for any comments
mark
similar to SortedDictionary.Remove(key)
how to do that with List<Criteria>.Remove( ? )
i found two ways so far, comments?
from http://stackoverflow.com/questions/2742313/list-remove-question
1)
....said to be using Link ?(of which i know exactly nothing)
Customer customer = collCustList.Where(c => c.Number == 99 && c.Type == "H"
/* etc */).FirstOrDefault();
if (customer != null)
{
collCustList.Remove(customer);
}
....or ....
2)
collCustList.RemoveAll(c => c.Number == 99 && c.Type == "H" /* etc */);
i'm using the .Where version and it seems to work.
//this fires when user checks an item in listview
private void lvCriteriaList_ItemCheck(object sender,
System.Windows.Forms.ItemCheckEventArgs e)
{
string key=this.lvCriteriaList.Items[e.Index].SubItems[1].Text;
string value=this.lvCriteriaList.Items[e.Index].Text;
if (_criteriaChoiceList == null)
{
_criteriaChoiceList = new List<Criteria>();
}
if (e.NewValue == CheckState.Checked)
{
AddCriteria(key, value);
}
else if (e.NewValue == CheckState.Unchecked)
{
RemoveCriteria(key, value);
}
DisplayCriteria();
}
private void AddCriteria(string key, string value )
{
_criteriaChoiceList.Add(new Criteria(key, value));
}
private void RemoveCriteria(string key, string value)
{
Criteria criteria1 = _criteriaChoiceList.Where(c => c.Key ==
key && c.Value == value ).FirstOrDefault();
if (criteria1 != null)
{
_criteriaChoiceList.Remove(criteria1);
}
}
thanks for any comments
mark