-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
54 lines (45 loc) · 1.45 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Newtonsoft.Json;
namespace SqlGadgetry
{
class Program
{
static void Main()
{
const string sql = "SELECT Name FROM Customers";
Console.WriteLine("SQL: {0}", sql);
var parser = new SqlParser();
LambdaExpression queryExp = parser.Parse<Context>(sql);
var context = new Context();
IEnumerable<dynamic> results = Execute(context, queryExp).Cast<dynamic>();
Console.WriteLine("Results: {0}", JsonConvert.SerializeObject(results));
Console.Write("Press any key to quit.");
Console.ReadKey(true);
}
private static IQueryable Execute<TContext>(TContext context, LambdaExpression queryExp)
{
Delegate queryMethod = queryExp.Compile();
return (IQueryable)queryMethod.DynamicInvoke(context);
}
}
public class Context
{
private readonly List<Customer> _customerRepository = new List<Customer>
{
new Customer { Name = "Joe", Age = 28 },
new Customer { Name = "Fred", Age = 28 }
};
public IQueryable<Customer> Customers
{
get { return _customerRepository.AsQueryable(); }
}
}
public class Customer
{
public string Name { get; set; }
public int Age { get; set; }
}
}