寫的搜索查詢我需要根據以下標準來寫的搜索:如何使用或代替而
我要找到匹配 key1的OR鍵2 OR關鍵的3個值的值的所有記錄。 ..等
鍵和值的數量是可變的
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"}),
};
現在我實現
我目前的實現搜索,但所有的表達式都是「AND」而不是或。我不知道如何編寫它。
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();
}
}
完整的例子,除了db是: https://gist.github.com/cpoDesign/acf69bc242ed0755597d
我最近通過EF實現搜索經驗使我相信,手工SQL是這樣的去尋找性能方面的原因。 –
@SamAxe也是我關心的,thnx的小費 – cpoDesign