Thursday, October 24, 2013

C# implementation of SQL IN statement


SQL functionality to mimic:
SELECT * FROM table WHERE number IN (16, 30, 78, 80)

C# example that will work similarly.
List<int> Numbers = new List<int> { 16, 30, 78, 80 };
int number = 78;
if (Numbers.Contains(number))
{
    //...
}

Note: To make the functionality more similar, a class could be created inheriting from List which exposes an "IN" method which would simply encapsulate the List.Contains method.

Sunday, October 20, 2013

C# copy a Queue so initial values remain.

public static T DeepClone<T>(T obj)
{
    using (var ms = new MemoryStream())
    {
        var formatter = new BinaryFormatter();
        formatter.Serialize(ms, obj); ms.Position = 0;
       
        return (T) formatter.Deserialize(ms);
    }
}

//The class object must be marked as Serializable.