Click here to Skip to main content
15,886,362 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.2K   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;
using LinqStatistics;

namespace SqlLinq.UnitTests
{
    [TestClass]
    public class GroupByTests
    {
        [TestMethod]
        public void GroupByIntoTuple()
        {
            IEnumerable<Person> source = TestData.GetPeople();
            var result = source.Query<Person, Tuple<string, double>>("SELECT Address, Avg(Age) FROM this GROUP BY Address");

            var answer = from p in source
                         group p by p.Address into g
                         select new Tuple<string, double>(g.Key, g.Average(p => p.Age));

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

        [TestMethod]
        public void GroupByAndCountIntoTuple()
        {
            IEnumerable<Person> source = TestData.GetPeople();
            var result = source.Query<Person, Tuple<string, double>>("SELECT Address, COUNT(*) FROM this GROUP BY Address");

            var answer = from p in source
                         group p by p.Address into g
                         select new Tuple<string, double>(g.Key, g.Count());

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

        [TestMethod]
        public void GroupByIntoNewObject()
        {
            IEnumerable<Person> source = TestData.GetPeople();
            var result = source.Query<Person, Family>("SELECT Address, Avg(Age) AS AverageAge FROM this GROUP BY Address");

            var answer = from p in source
                         group p by p.Address into g
                         select new Family { Address = g.Key, AverageAge = g.Average(p => p.Age) };

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

        [TestMethod]
        public void GroupByWithOrderBy()
        {
            IEnumerable<Person> source = TestData.GetPeople();
            var result = source.Query<Person, Family>("SELECT Address, Avg(Age) AS AverageAge FROM this GROUP BY Address ORDER BY Address");

            var answer = from p in source
                         group p by p.Address into g
                         select new Family { Address = g.Key, AverageAge = g.Average(p => p.Age) }
                             into f
                             orderby f.Address
                             select f;

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

        [TestMethod]
        public void GroupByIntoNewObjectTwoAggregates()
        {
            IEnumerable<Person> source = TestData.GetPeople();
            var result = source.Query<Person, Family>("SELECT Address, Avg(Age) AS AverageAge, Sum(Age) AS TotalAge FROM this GROUP BY Address");

            var answer = from p in source
                         group p by p.Address into g
                         select new Family { Address = g.Key, AverageAge = g.Average(p => p.Age), TotalAge = g.Sum(p => p.Age) };

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

        [TestMethod]
        public void GroupByIntoNewObjectDynamic()
        {
            IEnumerable<Person> source = TestData.GetPeople();
            var result = source.Query<Person, dynamic>("SELECT Address, Avg(Age) AS AverageAge FROM this GROUP BY Address");

            var answer = from p in source
                         group p by p.Address into g
                         select new Family { Address = g.Key, AverageAge = g.Average(p => p.Age) };

            IEnumerable<double> ages = result.Select<dynamic, double>(x => x.AverageAge);
            IEnumerable<double> ages1 = answer.Select(x => x.AverageAge);
            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(result.Any());
            Assert.IsTrue(answer.Any());
            Assert.IsTrue(addresses.SequenceEqual(addresses1));
        }

        [TestMethod]
        public void GroupByIntoDictionary()
        {
            IEnumerable<Person> source = TestData.GetPeople();
            var result = source.Query<Person, IDictionary<string, object>>("SELECT Address, Avg(Age) AS AverageAge FROM this GROUP BY Address");

            var answer = from p in source
                         group p by p.Address into g
                         select new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase) 
                         { 
                             {"Address", g.Key},
                             {"AverageAge", g.Average(p => p.Age)}
                         };

            Assert.IsTrue(answer.SequenceEqual(result, new DictionaryComparer<string, object>()));
        }
    }
}

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