Click here to Skip to main content
15,896,912 members
Articles / Programming Languages / C# 4.0

Dynamically evaluated SQL LINQ queries

Rate me:
Please Sign up or sign in to vote.
4.95/5 (35 votes)
30 Nov 2013CPOL8 min read 194.7K   2.6K   116  
Extension methods to evaluate plain text SQL queries against IEnumerable collections.
using System;
using System.Diagnostics;
using System.Linq.Expressions;

namespace SqlLinq.SyntaxTree.Joins
{
    /// <summary>
    /// These guys abstract away knowing how to get the values that will be
    /// joined on as expressions are built
    /// </summary>
    interface IJoinEntry
    {
        bool ContainsKey(string key);

        object this[string key] { get; }
    }

    class ObjectJoinEntry : IJoinEntry
    {
        private object _entry;

        public ObjectJoinEntry(object o)
        {
            Debug.Assert(o != null);

            _entry = o;
        }

        public bool ContainsKey(string key)
        {
            return _entry.GetType().HasPropertyOrField(key);
        }

        public object this[string key]
        {
            get
            {
                Debug.Assert(_entry.GetType().HasPropertyOrField(key));

                return _entry.GetPropertyOrFieldValue(key);
            }
        }
    }

    class DictionaryJoinEntry : IJoinEntry
    {
        private Func<string, object> _get_Item;
        private Func<string, bool> _containsKey;

        public DictionaryJoinEntry(object o, Type t)
        {
            Debug.Assert(o != null);

            _containsKey = CreateFunc<string, bool>(o, t, "ContainsKey");
            _get_Item = CreateFunc<string, object>(o, t, "get_Item");
        }

        public bool ContainsKey(string key)
        {
            return _containsKey(key);
        }

        public object this[string key]
        {
            get
            {
                return _get_Item(key);
            }
        }

        private static Func<TArg, TReturn> CreateFunc<TArg, TReturn>(object instance, Type t, string methodName)
        {
            var method = t.GetMethod(methodName, new Type[] { typeof(string) });
            Debug.Assert(method != null);

            ParameterExpression arg = Expression.Parameter(typeof(TArg), "arg");
            Expression call = Expression.Call(Expression.Constant(instance), method, arg);

            return Expression.Lambda<Func<TArg, TReturn>>(call, arg).Compile();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Team Leader Starkey Laboratories
United States United States
The first computer program I ever wrote was in BASIC on a TRS-80 Model I and it looked something like:
10 PRINT "Don is cool"
20 GOTO 10

It only went downhill from there.

Hey look, I've got a blog

Comments and Discussions