1

Say I have a list of training centres like

List<TrainingCentre> tList = new List<TrainingCentre>()

 {

     new TrainingCentre{ Id=1, Name="XXXX", location="NY"},
     new TrainingCentre{Id=2, Name="YYYY",location="OL"},
     new TrainingCentre{Id=3,Name="ZZZ",location="DD"} 

 };

each training centre has exactly one location.

The requirement here is to allow the user to key in their preferred locations and display the result according their search keywords.

For example :

User may prefer two locations "NY" and "DD" or single location like "NY".

How to write a where clause to take different locations at a time. Do i need string split or other techniques?

4

1 回答 1

1

I hope you are looking for something similar to the following

List<TrainingCentre> tList = new List<TrainingCentre>()
            {
                new TrainingCentre{ Id=1, Name="XXXX", location="NY"},
                new TrainingCentre{Id=2, Name="YYYY",location="OL"},
                new TrainingCentre{Id=3,Name="ZZZ",location="DD"} 

            };

//Simulated search keywords

 List<string> PreferredLocation = new List<string>{"NY","DD" };

//Searching for the preferred locations

var filter = from trainingcentre in tList
                      where PreferredLocation.Contains(trainingcentre.location)
                      select trainingcentre;
于 2013-06-19T03:28:13.413 回答