我可以扩展LINQ-to-SQL支持的运算符吗?[英] Can I extend the operators that LINQ-to-SQL supports?

本文是小编为大家收集整理的关于我可以扩展LINQ-to-SQL支持的运算符吗?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

如果我想足够糟糕,我可以将其他LINQ构造添加到Linq-to-SQL(如果创建自己的LINQ提供商,我可以做到)?例如,许多内置的LINQ运算符(XYZ.Any())直接转换为SQL(例如IF EXISTS(XYZ)).

如果我需要专门的结构,我什至可以扩大集合,还是必须烘烤到实际的LINQ到SQL提供商?

我将如何添加新的操作员实现? C#扩展方法足以完成工作吗?

如果答案是肯定的,那么有人倾向于通过linq-to-sql生成的动态SQL替换太多(全部?)的Sproc功能?

推荐答案

从理论上讲,您可以在LINQ提供商中添加对任意表达式的支持,但是Pavel是正确的,LINQ到SQL不能作为起点.我会研究亚音速或NHIBERNATE,它们也在开发LINQ提供商.一旦提供者支持表达式,您只需要在IQueryable<T>上构建等效扩展方法 - 反射System.Linq.Queryable以获取详细信息.

也就是说,解析表达树绝对不是胆小的.除非您对AST理论非常感兴趣,否则以"正常"方式扩展OSS可能会更容易.

其他推荐答案

否,Linq到SQL并非如此扩展.最好的方法是使用存储过程.

本文地址:https://www.itbaoku.cn/post/1556833.html

问题描述

If I wanted to badly enough, could I add additional LINQ constructs to LINQ-to-SQL (as I would be able to if creating my own LINQ provider)? For instance, many of the built-in LINQ operators (XYZ.Any()) get directly translated to SQL (e.g. IF EXISTS(XYZ)).

If I needed a specialized construct, would I even be able to augment the set, or does this have to be baked into the actual LINQ-to-SQL provider?

How would I go about adding a new operator implementation? Are C# extension methods enough of a hook to do the job?

If the answer is yes, would someone so inclined be able to replace much (all?) of sproc capability via the dynamic SQL generated by LINQ-to-SQL?

推荐答案

In theory you could add support for arbitrary expressions to a LINQ provider, but Pavel is correct that LINQ to SQL would not work as a starting point. I would look into SubSonic or NHibernate, which also have LINQ providers under development. Once the provider supports an expression, you would just need to build an equivalent extension method on IQueryable<T> - reflect System.Linq.Queryable for details.

That said, parsing expression trees is definitely not for the faint of heart. Unless you're really interested in AST theory, it would probably be easier to just extend an OSS ORM in a "normal" way.

其他推荐答案

No, LINQ to SQL is not extensible like that. The best you can do is to use stored procedures.