Solved

Using List<> FindAll with multiple criteria

Posted on 2008-10-07
5
637 Views
Last Modified: 2013-12-17
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
string office = ((ListBox)sender).SelectedValue;            
List<PartnerRecord> results = currentResults.FindAll(delegate(PartnerRecord pr1) 
                { return pr1.Office.Equals(office, StringComparison.CurrentCultureIgnoreCase); });

Open in new window

0
Comment
Question by:eshurak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 500 total points
ID: 22664537
this should do
string office = ((ListBox)sender).SelectedValue;
string dept = "some value";

List<PartnerRecord> results =
    currentResults.FindAll(delegate(PartnerRecord pr1)
                { return
                               pr1.Office.Equals(office, StringComparison.CurrentCultureIgnoreCase) &&
                               pr1.Department.Equals(dept, StringComparison.CurrentCultureIgnoreCase)
                 ; });
0
 
LVL 3

Author Comment

by:eshurak
ID: 22664651
It should also compinate for offfice or dept being null.  Right now I'm doing this.

            List<PartnerRecord> results = currentResults;
               
            if (DepartmentList.SelectedValue != "")                
                results = results.FindAll(delegate(PartnerRecord pr1)
            { return pr1.Department.Equals(DepartmentList.SelectedValue.ToString(), StringComparison.CurrentCultureIgnoreCase); });

            if (OfficeList.SelectedValue != "")
                results = results.FindAll(delegate(PartnerRecord pr1)
                { return pr1.Office.Equals(OfficeList.SelectedValue.ToString(), StringComparison.CurrentCultureIgnoreCase); });

It works be I've got four different types of criteria to look at and it's going to get ugly.
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22664847
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.CurrentCultureIgnoreCase) && pr1.Department.Equals (dept, StringComparison.CurrentCultureIgnoreCase);
                        });
}
else if (office != string.Empty)
{
      results = currentResults.FindAll (delegate (PartnerRecord pr1)
                        {
                              return pr1.Office.Equals (office, StringComparison.CurrentCultureIgnoreCase);
                        });                  
}
else if (dept != string.Empty)
{
      results = currentResults.FindAll (delegate (PartnerRecord pr1)
      {
            return pr1.Department.Equals (dept, StringComparison.CurrentCultureIgnoreCase);
      });
}
else
{
      results = currentResults;
}
0
 
LVL 3

Author Comment

by:eshurak
ID: 22670408
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.SelectedValue != "")
                results = results.FindAll(delegate(PartnerRecord pr1)
            { return pr1.Department.Equals(DepartmentList.SelectedValue.ToString(), StringComparison.CurrentCultureIgnoreCase); });

            if (OfficeList.SelectedValue != "")
                results = results.FindAll(delegate(PartnerRecord pr1)
                { return pr1.Office.Equals(OfficeList.SelectedValue.ToString(), StringComparison.CurrentCultureIgnoreCase); });

            if (RoleOrCommitteeList.SelectedValue != "")
                results = results.FindAll(delegate(PartnerRecord pr1)
                { return pr1.RoleOrCommittee.Contains(RoleOrCommitteeList.SelectedValue.ToString()); });

            if(isEquity.Checked)
                results = results.FindAll(delegate(PartnerRecord pr1)
                { return pr1.IsEquity.Equals("True",StringComparison.CurrentCultureIgnoreCase); });
0
 
LVL 3

Author Closing Comment

by:eshurak
ID: 31504041
Thanks again.  Even though I did not use your initial sugguestion it's good to know that I can if I need to.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question