if条件下C#中的多个条件
我是c#的新人。在我使用oracle之前。
任何更简单的方法来写这个if语句?
if(value == 1 || value == 2)
例如......在SQL中你可以说(1,2)中的值而不是value = 1或value = 2。
i想要尝试调用基于字符串的过程包含特定值,例如
p_value.startwith(414,413)
即p_value是41400或41300或41402等
请帮帮我
我的尝试:
i已经尝试了
value.contains(414,412)
{
一些代码;
}
或value.startwith(414,412)
{ \ some code
i am new comer in c#. before i am using oracle .
Any easier way to write this if statement?
if (value==1 || value==2)
For example... in SQL you can say where value in (1,2) instead of where value=1 or value=2.
i want to try to call procedure based on string contains particular value for eg
p_value.startwith("414","413")
ie p_value is 41400 or 41300 or 41402 etc
please help me
What I have tried:
i have tried
value.contains ("414","412")
{
some code;
}
or value.startwith("414","412")
{\some code
你可以使用下面的东西
You can use something like below
using System.Linq;
public static bool CheckIn<T>(this T item, params T[] Mylist)
{
return list.Contains(item);
}
var list = new List<int> { 1, 2, 3, 4, 5 };
if (!list .CheckIn(1,3,4))
Linq扩展包含两个方便的布尔方法 - 全部和任意。这些可以用于你的情况:
The Linq extensions have two handy boolean methods - All and Any. These could be used for your situation thus:
if (new[]{"414", "412", "410"}.Any(s=>value.StartsWith(s))) {
// Do your stuff
}
这是我为自己写的扩展方法:
Here's an extension method I wrote for myself:
public static bool IsIn(this string str, string container, bool exact=true)
{
bool result = !string.IsNullOrEmpty(str);
if (result)
{
result = (exact) ? (str == container) || container.IndexOf(str) >= 0
: str.ToUpper() == container.ToUpper() || container.ToUpper().IndexOf(str.ToUpper()) >= 0;
}
return result;
}
用法是这样的:
Usage would be something like this:
string badStuff = "Text,we,don't,want";
string ourData = "text";
bool thisIsFalse = ourData.IsIn(badStuff); // because "text" is lowercase and
// exact is true
bool thisIsTrue = ourData.IsIn(badStuff, false); // because exact is false, so
// case sensitivity is irrelevant
这个想法可以使用 IEnumerable< string>
,甚至 IEnumerabe< YourObject>,字符串属性名
作为参数进入此方法的重载。
您甚至可以使用其他类型:
This idea can be carried into overloads of this method with IEnumerable<string>
, or even IEnumerabe<YourObject>, string propertyname
as parameters.
You could even do it with other types:
public static bool IsIn(int this value, string container)
{
return value.ToString().IsIn(container);
}