Click here to Skip to main content
15,885,914 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 192.1K   2.6K   116  
Extension methods to evaluate plain text SQL queries against IEnumerable collections.
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace QueryAnything.UnitTests
{
    [TestClass]
    public class DynamicTests
    {
        [TestMethod]
        public void DynamicOneProperty()
        {
            IEnumerable<Person> source = TestData.GetPeople();
            var result = source.Query<Person, dynamic>("SELECT address FROM this");
            Assert.IsTrue(result.SequenceEqual(source.Select(p => p.Address)));
        }

        [TestMethod]
        public void DynamicTwoProperties()
        {
            IEnumerable<Person> source = TestData.GetPeople();
            var result = source.Query<Person, dynamic>("SELECT age, address FROM this");

            var answer = from p in source
                         select new { p.Age, p.Address };

            IEnumerable<int> ages = result.Select<dynamic, int>(x => x.age);
            IEnumerable<int> ages1 = answer.Select(x => x.Age);
            Assert.IsTrue(ages.SequenceEqual(ages1));

            IEnumerable<string> addresses = result.Select<dynamic, string>(x => x.address);
            IEnumerable<string> addresses1 = answer.Select(x => x.Address);
            Assert.IsTrue(addresses.SequenceEqual(addresses1));
        }

        [TestMethod]
        public void NewObjectTwoProperties()
        {
            IEnumerable<Person> source = TestData.GetPeople();
            var result = source.Query<Person, BasicPerson>("SELECT name, address FROM this");

            var answer = from p in source select new BasicPerson { Name = p.Name, Address = p.Address };

            Assert.IsTrue(result.SequenceEqual(answer));
        }

        [TestMethod]
        public void NewObjectTwoPropertiesWithAs()
        {
            IEnumerable<Person> source = TestData.GetPeople();
            var result = source.Query<Person, OtherPerson>("SELECT name, address AS location FROM this");

            var answer = from p in source select new OtherPerson { Name = p.Name, Location = p.Address };

            Assert.IsTrue(result.SequenceEqual(answer));
        }

        [TestMethod]
        public void NewObjectTwoPropertiesConstructor()
        {
            IEnumerable<Person> source = TestData.GetPeople();
            var result = source.Query<Person, Tuple<string, string>>("SELECT name, address FROM this");

            var answer = from p in source select new Tuple<string, string>(p.Name,  p.Address );

            Assert.IsTrue(result.SequenceEqual(answer));
        }

        [TestMethod]
        public void NewObjectThreePropertiesConstructor()
        {
            IEnumerable<Person> source = TestData.GetPeople();
            var result = source.Query<Person, Tuple<string, int, string>>("SELECT name, age, address FROM this");

            var answer = from p in source select new Tuple<string, int, string>(p.Name, p.Age, p.Address);

            Assert.IsTrue(result.SequenceEqual(answer));
        }
    }
}

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