如何将Lambda表达式转换为Sql?

我正在开发一个小框架来访问数据库。 我想添加一个使用lambda表达式进行查询的function。 我该怎么做呢?

public class TestModel { public int Id {get;set;} public string Name {get;set;} } public class Repository { // do something. } 

例如:

 var repo = new Repository(); var query = repo.AsQueryable().Where(x => x.Name == "test"); // This query must be like this: // SELECT * FROM testmodel WHERE name = 'test' var list = query.ToDataSet(); // When I call ToDataSet(), it will get the dataset after running the made query. 

继续创建LINQ提供程序 (我确信你不想这样做,无论如何)。

这是很多工作,所以也许你只想使用NHibernate或Entity Framework或类似的东西。

如果您的查询相当简单,也许您不需要一个完整的LINQ提供程序。 看看Expression Trees (LINQ Providers使用)。

你可以破解这样的东西:

 public static class QueryExtensions { public static IEnumerable Where(this Repo source, Expression> predicate) { // hacks all the way dynamic operation = predicate.Body; dynamic left = operation.Left; dynamic right = operation.Right; var ops = new Dictionary(); ops.Add(ExpressionType.Equal, "="); ops.Add(ExpressionType.GreaterThan, ">"); // add all required operations here // Instead of SELECT *, select all required fields, since you know the type var q = String.Format("SELECT * FROM {0} WHERE {1} {2} {3}", typeof(TSource), left.Member.Name, ops[operation.NodeType], right.Value); return source.RunQuery(q); } } public class Repo { internal IEnumerable RunQuery(string query) { return new List(); // run query here... } } public class TestModel { public int Id { get; set; } public string Name { get; set; } } class Program { static void Main(string[] args) { var repo = new Repo(); var result = repo.Where(e => e.Name == "test"); var result2 = repo.Where(e => e.Id > 200); } } 

请不要使用它。 这只是一个快速而又脏的示例,可以分析表达式树来创建SQL语句。

为什么不使用Linq2Sql,NHibernate或EntityFramework …

如果你想做的事情

 db.Employee .Where(e => e.Title == "Spectre") .Set(e => e.Title, "Commander") .Update(); 

要么

 db .Into(db.Employee) .Value(e => e.FirstName, "John") .Value(e => e.LastName, "Shepard") .Value(e => e.Title, "Spectre") .Value(e => e.HireDate, () => Sql.CurrentTimestamp) .Insert(); 

要么

 db.Employee .Where(e => e.Title == "Spectre") .Delete(); 

然后看看这个, BLToolkit

您可能想查看http://iqtoolkit.codeplex.com/这是非常复杂的,我不建议您从头开始构建一些东西。

我只是写了一些接近dkons答案的东西,无论如何我会添加它。 只是使用流畅的界面而已。

 public class Query where T : class { private Dictionary _dictionary; public Query() { _dictionary = new Dictionary(); } public Query Eq(Expression> property) { AddOperator("Eq", property.Name); return this; } public Query StartsWith(Expression> property) { AddOperator("Sw", property.Name); return this; } public Query Like(Expression> property) { AddOperator("Like", property.Name); return this; } private void AddOperator(string opName, string prop) { _dictionary.Add(opName,prop); } public void Run(T t ) { //Extract props of T by reflection and Build query } } 

让我们说你有一个类似的模型

 class Model { public string Surname{ get; set; } public string Name{ get; set; } } 

您可以将其用作:

 static void Main(string[] args) { Model m = new Model() {Name = "n", Surname = "s"}; var q = new Query(); q.Eq(x => x.Name).Like(x=>x.Surname).Run(m); }