什么是Oracle IN语句的C#等价物?

时间:2012-08-29 21:59:47

标签: c# oracle syntax

如何检查变量是否为少数值之一?

Oracle中IN的位置如

where strVar in ('A','H','X','Z')

无需编写

if (strVar == "A" || strVar == "H" || strVar == "X" || strVar =="Z") 

编辑以更改变量名称

3 个答案:

答案 0 :(得分:7)

这对你来说应该足够了:

if(new string[]{"A","B",".."}.Contains(strVar)) {
    .... CONTAINS ...
}

也可以使用

if(new string[]{"A","B",".."}.Any(strVar)) {
    .... CONTAINS ...
}

Contains也适用C# 2.0,而Any没有。

编辑

正确地指出@brendan,摆脱var命名变量,即C#关键字,让我们避免造成混淆。

答案 1 :(得分:2)

这应该是你最好的选择:

if ("hxza".Contains(value))

我用一些不同的方法做了一些“测试速度”来做你想做的事情。

您可以在底部看到代码。请注意 X是时间我重复测试以获得平均值

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();

答案 2 :(得分:0)

没有任何背景:

new [] {'A','H','X','Z'}.Contains(var);