Created
July 10, 2015 13:13
-
-
Save cpoDesign/acf69bc242ed0755597d to your computer and use it in GitHub Desktop.
Search for keypairvalue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Test | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
List<KeyValuePair<string, string[]>> filterlist = new List<KeyValuePair<string, string[]>>() | |
{ | |
new KeyValuePair<string, string[]>("Key1", new []{"jay","bloggs"}), | |
new KeyValuePair<string, string[]>("Key2", new []{"joe","blog","doe"}), | |
new KeyValuePair<string, string[]>("Key3", new []{"jon","blog"}), | |
}; | |
var searchResults = UserSearcher.SearchProfiles(filterlist); | |
Console.WriteLine(searchResults); | |
} | |
public class UserSearcher | |
{ | |
private List<UserProfile> userProfiles; | |
public UserSearcher() | |
{ | |
userProfiles = new List<UserProfile>(); | |
} | |
public static List<UserProfile> SearchProfiles(List<KeyValuePair<string, string[]>> filterList) | |
{ | |
var list = new List<UserProfile>(); | |
var query = list.AsQueryable(); | |
// search for each pair inside as or | |
foreach (KeyValuePair<string, string[]> searchPair in filterList) | |
{ | |
foreach (string searchString in searchPair.Value) | |
{ | |
string s = searchString; | |
// search for each item inside as and (has to contains all search strings | |
query = query.Where(x => x.PersonName.Contains(s)); | |
} | |
} | |
return list = query.ToList(); | |
} | |
} | |
public class UserProfile | |
{ | |
public string PersonName { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment