我將如何檢查變量是否是幾個值之一?什麼是Oracle IN語句的C#等價物?
凡IN
在甲骨文會讀到這樣
where strVar in ('A','H','X','Z')
,而無需編寫
if (strVar == "A" || strVar == "H" || strVar == "X" || strVar =="Z")
editted改變變量名
我將如何檢查變量是否是幾個值之一?什麼是Oracle IN語句的C#等價物?
凡IN
在甲骨文會讀到這樣
where strVar in ('A','H','X','Z')
,而無需編寫
if (strVar == "A" || strVar == "H" || strVar == "X" || strVar =="Z")
editted改變變量名
這應該是夠你:
if(new string[]{"A","B",".."}.Contains(strVar)) {
.... CONTAINS ...
}
也還可以使用上C# 2.0
if(new string[]{"A","B",".."}.Any(strVar)) {
.... CONTAINS ...
}
但Contains
的作品,而不是Any
沒有。
編輯
由於正確地指出@brendan,擺脫var
命名變量,即C#
關鍵字,讓我們避免產生混淆。
沒有任何背景:
new [] {'A','H','X','Z'}.Contains(var);
這當然會用於查找字符。我沒有犯錯。你什麼都看不到。 –
這也許應該是你最好的選擇:
if ("hxza".Contains(value))
我做薩姆 「測試速度」 一些diferents方法可以做到你whant什麼去做。
您可以看到底部爲的代碼。需要注意的是X爲I重複測試,使平均
結果爲X = 1的時間數:
Average of ticks for test7 : 4
Average of ticks for test2 : 5
Average of ticks for test10 : 6
Average of ticks for test9 : 7
Average of ticks for test4 : 19
Average of ticks for test8 : 35
Average of ticks for test6 : 451
Average of ticks for test5 : 2711
Average of ticks for test1 : 3146
Average of ticks for test3 : 8569
結果爲X = 100:
Average of ticks for test2 : 3
Average of ticks for test7 : 3
Average of ticks for test10 : 3
Average of ticks for test9 : 5
Average of ticks for test4 : 5
Average of ticks for test8 : 5
Average of ticks for test6 : 11
Average of ticks for test3 : 20
Average of ticks for test5 : 23
Average of ticks for test1 : 35
結果對於X = 100 000:
Average of ticks for test2 : 3
Average of ticks for test7 : 3
Average of ticks for test5 : 3
Average of ticks for test1 : 3
Average of ticks for test6 : 3
Average of ticks for test10 : 3
Average of ticks for test9 : 5
Average of ticks for test4 : 5
Average of ticks for test8 : 5
Average of ticks for test3 : 5
所以我們可以告訴測試2,7和10是讓你想要什麼好辦法:
//test2
if (value == 'a' || value == 'h' || value == 'x' || value == 'z')
//test7
if (value == 'h' || value == 'x' || value == 'z' || value == 'a')
//test10
if ("hxza".Contains(value))
看到代碼:
char value = 'a';
Dictionary<string, long> tests = new Dictionary<string, long>()
{
{"test1", 0},
{"test2", 0},
{"test3", 0},
{"test4", 0},
{"test5", 0},
{"test6", 0},
{"test7", 0},
{"test8", 0},
{"test9", 0},
{"test10", 0}
};
Stopwatch watch = new Stopwatch();
// tested with 1, 100 and 100 000
long X = 1;
for (int i = 0; i < X; i++)
{
watch.Start();
if ("ahxz".Any(c => c == value))
{
}
watch.Stop();
tests["test1"] += watch.ElapsedTicks;
watch.Reset();
watch.Start();
if (value == 'a' || value == 'h' || value == 'x' || value == 'z')
{
}
watch.Stop();
tests["test2"] += watch.ElapsedTicks;
watch.Reset();
watch.Start();
if (new[] { 'a', 'h', 'x', 'z' }.Contains(value))
{
}
watch.Stop();
tests["test3"] += watch.ElapsedTicks;
watch.Reset();
watch.Start();
if (new[] { 'a', 'h', 'x', 'z' }.Contains(value))
{
}
watch.Stop();
tests["test4"] += watch.ElapsedTicks;
watch.Reset();
watch.Start();
if ("ahxz".Contains(value))
{
}
watch.Stop();
tests["test5"] += watch.ElapsedTicks;
watch.Reset();
watch.Start();
if ("hxza".Any(c => c == value))
{
}
watch.Stop();
tests["test6"] += watch.ElapsedTicks;
watch.Reset();
watch.Start();
if (value == 'h' || value == 'x' || value == 'z' || value == 'a')
{
}
watch.Stop();
tests["test7"] += watch.ElapsedTicks;
watch.Reset();
watch.Start();
if (new[] { 'h', 'x', 'z', 'a' }.Contains(value))
{
}
watch.Stop();
tests["test8"] += watch.ElapsedTicks;
watch.Reset();
watch.Start();
if (new[] { 'h', 'x', 'z', 'a' }.Contains(value))
{
}
watch.Stop();
tests["test9"] += watch.ElapsedTicks;
watch.Reset();
watch.Start();
if ("hxza".Contains(value))
{
}
watch.Stop();
tests["test10"] += watch.ElapsedTicks;
watch.Reset();
}
foreach (var test in tests.OrderBy(p => p.Value))
{
Console.WriteLine("Average of ticks for {0} : {1}\n", test.Key, test.Value/nbrOfTimes);
}
Console.ReadLine();
自己寫的。在方法:) – Randy
@Randy:像[這]( http://stackoverflow.com/a/833477/284240)? –