Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 379
Nick Avatar asked Sep 03 '09 21:09

Nick


People also ask

What is the alternative for in in SQL?

An alternative for IN and EXISTS is an INNER JOIN, while a LEFT OUTER JOIN with a WHERE clause checking for NULL values can be used as an alternative for NOT IN and NOT EXISTS.

Is there any alternative for SQL?

We have compiled a list of solutions that reviewers voted as the best overall alternatives and competitors to Microsoft SQL Server, including Teradata Vantage, MySQL, Oracle Database, and IBM Db2.

Does SQL use .NET framework?

SQL Server 2014 and SQL Server 2012 use . Net Framework 3.5 SP1, which is supported till 2029, so this retirement doesn't impact them.

Can you use SQL in C#?

C# can execute 'SQL' select command against the database. The 'SQL' statement can be used to fetch data from a specific table in the database. Inserting data into the database – C# can also be used to insert records into the database.


1 Answers

new [] {10, 11, 12}.Contains(productTypeId)
like image 59
mmx Avatar answered Oct 16 '22 07:10

mmx