Nick
Nick

Reputation: 5908

Something similar to sql IN statement within .NET framework?

I have this function:

    public bool IsValidProduct(int productTypeId)
    {
        bool isValid = false;

        if (productTypeId == 10 ||
            productTypeId == 11 ||
            productTypeId == 12)
        {
            isValid = true;
        }

        return isValid;
    }

but I'm wondering if there's an easier way to write it, such as:

    public bool IsValidProduct(int productTypeId)
    {
        bool isValid = false;

        if (productTypeId.In(10,11,12))
        {
            isValid = true;
        }

        return isValid;
    }

I know I could write an extension method to handle this, I'm just curious if there's already something out there or if there's a better way to write it.

Upvotes: 6

Views: 303

Answers (8)

Crispy
Crispy

Reputation: 5637

This would work if the numbers you want are all in a range, not quite "IN" functionallity, but you may find it useful.

if (Enumerable.Range(10, 3).Contains(productTypeId))

Upvotes: 0

JontyMC
JontyMC

Reputation: 6068

var validIds = new[] { 10, 11, 12 };
validIds.Contains(productId);

Upvotes: 0

JaredPar
JaredPar

Reputation: 754763

Nope I don't believe so but you could write an extension method like so which allows your code to work exactly as written.

public static bool In<T>(this T source, params T[] args) {
    return args.Contains(source);
}

The array overhead is a bit much though. You would probably want to add special cases for a smaller fixed number of parameters.

Upvotes: 5

Quintin Robinson
Quintin Robinson

Reputation: 82345

You could do something along the lines of:

new int[] { 10,11,12 }.Contains(productTypeID);

or go further an create an extension for int along the lines of:

public static bool In(this int i, params int[] ints)
{
    return ints.Contains(i);
}

With a usage of:

if (productTypeID.In(10,11,12)) { ... }

I wouldn't say they are the most efficient, but possible yes.

Upvotes: 2

Botz3000
Botz3000

Reputation: 39620

Hmm, you could do this:

public bool IsValidProduct(int productTypeId)
{
    bool isValid = false;

    if (new[] {10,11,12}.Contains(productTypeId))
    {
        isValid = true;
    }

    return isValid;
}

or if you want the same thing shorter:

public bool IsValidProduct(int productTypeId)
{
    return (new[] {10,11,12}.Contains(productTypeId));
}

Upvotes: 3

Lee
Lee

Reputation: 144136

I don't think there's anything in the framework so you'll have to write your own extension method. The closest you can get with linq is something like:


if(new[] { 10, 11, 12 }.Contains(productTypeId)) { ... }

Upvotes: 1

Judah Gabriel Himango
Judah Gabriel Himango

Reputation: 60021

You can use the Contains extension method from .NET 3.5. It works on all enumerable types:

if (new [] { 10, 11, 12 }.Contains(productTypeId))
{
    ...
}

Upvotes: 0

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422016

new [] {10, 11, 12}.Contains(productTypeId)

Upvotes: 11

Related Questions