SQL Style Extensions for C#

(Is this serious? If you can work it out, please tell me. In any case, it's inspired by the fact I've been doing more SQL than C# lately)

Are you addicted to SQL? Are you uncomfortable whenever you have to use pesky imperative languages like C#?

Now you can use your favourite SQL functions inside C#, with the amazing new "SQL Style Extensions" class!

Need to know if a value is in a short list of constants?

In SQL you'd simply write "where State in ('QLD','NSW','VIC')" � but, until recently C# forced you to write:

if (State == "QLD" || State == "NSW" || State == "VIC")

But now -- thanks to the aMAZzing new 'SQL Style Extensions' -- you can simply write:

If (State.In("QLD","NSW","VIC"))

... then close down the IDE and get back to your Entity Diagrams in no time!

Do you miss the power of LIKE matching?

Miss it no more!

With SQL Style Extensions you can write:

If (Firstname.Like("Fred%")) and find all your Freds, Fredericks and Fredas in one powerful line!

Buy now!! <-- for added effect, imagine that text is blinking. I... just... couldn't... bring myself to apply a blink tag.

This is just a taste:

    using System.Linq;
    using System.Text.RegularExpressions;
 
    static class SqlStyleExtensions
    {
        public static bool In(this string me, params string[] set)
        {
            return set.Contains(me);
        }
                               
        public static bool Like(this string me, string pattern)
        { 
            //Turn a SQL-like-pattern into regex, by turning '%' into '.*'
			//Doesn't handle SQL's underscore into single character wild card '.{1,1}',
            //        or the way SQL uses square brackets for escaping.
            //(Note the same concept could work for DOS-style wildcards (* and ?)
            var regex = new Regex("^" + pattern
                           .Replace(".", "\\.")
                           .Replace("%",".*")
                           .Replace("\\.*","\\%")
                           + "$");
	
            return regex.IsMatch(me);
        }
    }

(Actually, that's the whole thing.)

(If you do use this, you might want to improve that regex. Testing was... rudimentary.)

 

I'm currently writing a book about how to build your first product. If you want to build your first product, please sign up to be notified when the book is available.

(By the way, I read every comment and often respond.)

Your comment, please?

Your Name
Your Url (optional)
Note: I may edit, reuse or delete your comment. Don't be mean.