Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
Posted
Updated 17-Oct-16 3:38am
v2
Comments
PIEBALDconsult 17-Oct-16 0:55am    
I'd use a HashSet.
Philippe Mori 17-Oct-16 9:09am    
If I have only 2 conditions and don't expect that to change in future, I would stay with your original code which is still the easiest to understand and the more efficient.

You can use something like below

C#
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))
 
Share this answer
 
Comments
forest4ever 17-Oct-16 0:56am    
sorry sir, it cannot work in my program . i want to check 3 strings in code
if substring(act_valve,1,3) in ("414","412","410")
{
generete_code()
} else
generate_para()
}
please help me
The Linq extensions have two handy boolean methods - All and Any. These could be used for your situation thus:

C#
if (new[]{"414", "412", "410"}.Any(s=>value.StartsWith(s))) {
   // Do your stuff
}
 
Share this answer
 
v2
Here's an extension method I wrote for myself:

C#
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:

C#
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 


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:

C#
public static bool IsIn(int this value, string container)
{
    return value.ToString().IsIn(container);
}
 
Share this answer
 
v3
Here are some extension methods that let you overload the method I posted earlier.

C#
public static bool IsIn(this string str, IEnumerable<string> list, bool caseSensitive = true)
{
    return (caseSensitive) ? (list.Where(x=>x==str).FirstOrDefault() != null) 
                            : (list.Where(x=>x.ToUpper() == str.ToUpper()).FirstOrDefault() != null);
}

public static bool IsIn<T>(this string str, IEnumerable<T> list, string propertyName, bool caseSensitive = true)
{
    Type itemType = typeof(T);
    PropertyInfo info = itemType.GetPropertyInfo(propertyName, TypeChecker.String);
    if (info == null)
    {
        throw new InvalidOperationException(string.Format("Named property ({0}) is not a string.", propertyName));
    }
    return (caseSensitive) ? (list.Where(x=>info.GetValue(x).ToString()==str).FirstOrDefault() != null) 
                            : (list.Where(x=>info.GetValue(x).ToString().ToUpper() == str.ToUpper()).FirstOrDefault() != null);
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900