與LINQ方法Where
list.Where(x => x.YourKey.Contains(searchString))
實施例1
List<Users> _users = AllUsers.Where(x => x.Name.Contains("sam"));
例2
List<Customers> _cust = AllCustomers.Where(x => x.City.Contains("London"));
你可以這樣寫,否則的方法:
public List<T> LikeSearch<T>(List<T> list, Func<T, string> getKey, string searchString)
{
return list.Where(x => getKey(x).Contains(searchString)).ToList();
}
而且你可以使用它像這樣:
例1
List<Users> _users = LikeSearch(AllUsers, x => x.Name, "sam");
例2
List<Customers> _cust = LikeSearch(AllCustomers, x => x.City, "London");
編輯:下面關於解決方案的小基準這裏提出
我只爲基準的Contains
版本大家。
有了這個,我們可以看到(根據您的計算機和星星......上):
其間OneProperty:00:00:00.0026050
Moumit OneProperty:00:00:00.0013360
Mine OneProperty:00:00:00。0010390
兩個不同類別來這裏是爲了測試是否屬性的數目改變的東西
其間LotProperties:00:00:00.0026378
Moumit LotProperties:00:00:00.0012155
礦區性質:00:00:00.0010021
我很驚訝Moumit的解決方案是如何快速的,我預計它在運行時編譯會更慢。但是,儘管如此,我們可以看到GetProperty
和GetValue
確實很慢。
基準代碼:
static void Main(string[] args)
{
int size = 10000;
Dictionary<string, List<long>> time = new Dictionary<string, List<long>>()
{
{"InBetween OneProperty", new List<long>() },
{"Moumit OneProperty", new List<long>() },
{"Mine OneProperty", new List<long>() },
{"InBetween LotProperties", new List<long>() },
{"Moumit LotProperties", new List<long>() },
{"Mine LotProperties", new List<long>() },
};
List<OneProperty> oneProperties = new List<OneProperty>();
List<LotProperties> lotProperties = new List<LotProperties>();
for (int i = 0; i < size; ++i)
{
oneProperties.Add(new OneProperty() { Key = i.ToString() });
lotProperties.Add(new LotProperties() { Key = i.ToString() });
}
Stopwatch sw = new Stopwatch();
for (int i = 0; i < 1000; ++i)
{
sw.Start();
InBetween.LikeSearch(oneProperties, "Key", "999");
sw.Stop();
time["InBetween OneProperty"].Add(sw.Elapsed.Ticks);
sw.Reset();
sw.Start();
Moumit.Filter(oneProperties, "Key", "999");
sw.Stop();
time["Moumit OneProperty"].Add(sw.Elapsed.Ticks);
sw.Reset();
sw.Start();
Mine.LikeSearch(oneProperties, x => x.Key, "999");
sw.Stop();
time["Mine OneProperty"].Add(sw.Elapsed.Ticks);
sw.Reset();
sw.Start();
InBetween.LikeSearch(lotProperties, "Key", "999");
sw.Stop();
time["InBetween LotProperties"].Add(sw.Elapsed.Ticks);
sw.Reset();
sw.Start();
Moumit.Filter(lotProperties, "Key", "999");
sw.Stop();
time["Moumit LotProperties"].Add(sw.Elapsed.Ticks);
sw.Reset();
sw.Start();
Mine.LikeSearch(lotProperties, x => x.Key, "999");
sw.Stop();
time["Mine LotProperties"].Add(sw.Elapsed.Ticks);
sw.Reset();
}
foreach (string key in time.Keys)
Console.WriteLine($"{key}: {new TimeSpan((long)time[key].Average())}");
Console.ReadKey();
}
class OneProperty
{
public string Key { get; set; }
}
class LotProperties
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public string D { get; set; }
public string E { get; set; }
public string F { get; set; }
public string G { get; set; }
public string H { get; set; }
public string I { get; set; }
public string J { get; set; }
public string K { get; set; }
public string L { get; set; }
public string M { get; set; }
public string N { get; set; }
public string O { get; set; }
public string P { get; set; }
public string Q { get; set; }
public string R { get; set; }
public string S { get; set; }
public string T { get; set; }
public string U { get; set; }
public string V { get; set; }
public string W { get; set; }
public string X { get; set; }
public string Y { get; set; }
public string Z { get; set; }
public string Key { get; set; }
}
爲什麼'key'類型'T '?它似乎是一個屬性名稱,它不應該總是一個「字符串」?這可以通過反射來解決。它會很慢... – InBetween
是的,正確的抱歉....鍵將始終在這裏字符串... –
看看[動態LINQ](http://weblogs.asp.net/scottgu/動態LINQ部分-1-使用最LINQ的動態查詢的庫)。 –