开发者

Convert lambda expressions to SQL in an ORM?

开发者 https://www.devze.com 2023-02-28 02:25 出处:网络
How could I write an ORM that would convert LINQ into SQL? I already have made an existing ORM, but I want to improve it with LINQ, so that I, for instance would be able to say:

How could I write an ORM that would convert LINQ into SQL?

I already have made an existing ORM, but I want to improve it with LINQ, so that I, for instance would be able to say:

MyORMObject.GetAll(o => o.firstName == "peter");

The idea in my head is that the system would then take that and convert it in开发者_运维知识库to a query. I guess the hard part is to read the stuff parsed into the LINQ part.

How do I do this? In other words, how would I (through Reflection or something else) read the FirstName property being used, and its desired match, "Peter"?


You are going to need to implement an IQueryable LINQ Provider. You won't be using reflection by the way, you'll be using Expression trees.


If your method is IEnumerable<T> GetAll<T>(Expression<Func<T,bool>>), then the lambda expression will be compiled as an expression tree, with an Equals sub-expression, which in turn has a MemberExpression containing the FirstName property, and a ConstantExpression containing the string.

0

精彩评论

暂无评论...
验证码 换一张
取 消