eshurak
asked on
Using List<> FindAll with multiple criteria
Hello,
Forgive me if my terminology is not correct. I am using the code below to return results from a list. Currently it only uses the "Office" field as criteria how can I add multiple criteria. For example have it use "Office" and "department".
Thanks
Forgive me if my terminology is not correct. I am using the code below to return results from a list. Currently it only uses the "Office" field as criteria how can I add multiple criteria. For example have it use "Office" and "department".
Thanks
string office = ((ListBox)sender).SelectedValue;
List<PartnerRecord> results = currentResults.FindAll(delegate(PartnerRecord pr1)
{ return pr1.Office.Equals(office, StringComparison.CurrentCultureIgnoreCase); });
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
what is your exact requirement
if office and department is present then return on the basis of both
if office is selected and dept is blank then also return results
if office is blank and dept is selected then also return results
and if both are blank then you already have the list
this can be achieved in the if elseif block easily (good for readilibity aslo)
string office = string.Empty;
string dept = string.Empty;
List<PartnerRecord> results = new List<PartnerRecord>();
if (office != string.empty && dept != string.empty)
{
results = currentResults.FindAll (delegate (PartnerRecord pr1)
{
return pr1.Office.Equals (office, StringComparison.CurrentCu ltureIgnor eCase) && pr1.Department.Equals (dept, StringComparison.CurrentCu ltureIgnor eCase);
});
}
else if (office != string.Empty)
{
results = currentResults.FindAll (delegate (PartnerRecord pr1)
{
return pr1.Office.Equals (office, StringComparison.CurrentCu ltureIgnor eCase);
});
}
else if (dept != string.Empty)
{
results = currentResults.FindAll (delegate (PartnerRecord pr1)
{
return pr1.Department.Equals (dept, StringComparison.CurrentCu ltureIgnor eCase);
});
}
else
{
results = currentResults;
}
if office and department is present then return on the basis of both
if office is selected and dept is blank then also return results
if office is blank and dept is selected then also return results
and if both are blank then you already have the list
this can be achieved in the if elseif block easily (good for readilibity aslo)
string office = string.Empty;
string dept = string.Empty;
List<PartnerRecord> results = new List<PartnerRecord>();
if (office != string.empty && dept != string.empty)
{
results = currentResults.FindAll (delegate (PartnerRecord pr1)
{
return pr1.Office.Equals (office, StringComparison.CurrentCu
});
}
else if (office != string.Empty)
{
results = currentResults.FindAll (delegate (PartnerRecord pr1)
{
return pr1.Office.Equals (office, StringComparison.CurrentCu
});
}
else if (dept != string.Empty)
{
results = currentResults.FindAll (delegate (PartnerRecord pr1)
{
return pr1.Department.Equals (dept, StringComparison.CurrentCu
});
}
else
{
results = currentResults;
}
ASKER
Thanks for your help. I went with a series of if statements instead or an if else. If me it's easier to read and simplier to edit if things change.
if (DepartmentList.SelectedVa lue != "")
results = results.FindAll(delegate(P artnerReco rd pr1)
{ return pr1.Department.Equals(Depa rtmentList .SelectedV alue.ToStr ing(), StringComparison.CurrentCu ltureIgnor eCase); });
if (OfficeList.SelectedValue != "")
results = results.FindAll(delegate(P artnerReco rd pr1)
{ return pr1.Office.Equals(OfficeLi st.Selecte dValue.ToS tring(), StringComparison.CurrentCu ltureIgnor eCase); });
if (RoleOrCommitteeList.Selec tedValue != "")
results = results.FindAll(delegate(P artnerReco rd pr1)
{ return pr1.RoleOrCommittee.Contai ns(RoleOrC ommitteeLi st.Selecte dValue.ToS tring()); });
if(isEquity.Checked)
results = results.FindAll(delegate(P artnerReco rd pr1)
{ return pr1.IsEquity.Equals("True" ,StringCom parison.Cu rrentCultu reIgnoreCa se); });
if (DepartmentList.SelectedVa
results = results.FindAll(delegate(P
{ return pr1.Department.Equals(Depa
if (OfficeList.SelectedValue != "")
results = results.FindAll(delegate(P
{ return pr1.Office.Equals(OfficeLi
if (RoleOrCommitteeList.Selec
results = results.FindAll(delegate(P
{ return pr1.RoleOrCommittee.Contai
if(isEquity.Checked)
results = results.FindAll(delegate(P
{ return pr1.IsEquity.Equals("True"
ASKER
Thanks again. Even though I did not use your initial sugguestion it's good to know that I can if I need to.
ASKER
List<PartnerRecord> results = currentResults;
if (DepartmentList.SelectedVa
results = results.FindAll(delegate(P
{ return pr1.Department.Equals(Depa
if (OfficeList.SelectedValue != "")
results = results.FindAll(delegate(P
{ return pr1.Office.Equals(OfficeLi
It works be I've got four different types of criteria to look at and it's going to get ugly.