如何创建询问服务转换为SQL的方法? [英] How to create a method that suport translation to sql?

查看:218
本文介绍了如何创建询问服务转换为SQL的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我的查询堂妹内创建的方法我需要实现一种特殊类型的过滤器...

I wanted to use a method I created inside a query coz i need to implement a peculiar type of filter...

return manager.Clients.SelectAll().Where(cli => cli.Name.SatisfyFilter(filter.Name) && cli.LastName.SatisfyFilter(filter.LastName) && cli.MiddleName.SatisfyFilter(filter.MiddleName)).ToList();



但我得到的:

But i get the:

方法'布尔SatisfyFilter(System.String,System.String)没有支持转换为SQL。

"Method 'Boolean SatisfyFilter(System.String, System.String)' has no supported translation to SQL."

错误

我的方法是:

public static bool SatisfyFilter(this string palavra, string filtro)

同样的事情

public bool Contains(string value)

在字符串类型,包含作品就好了...

in the string type, and Contains works just fine...

我需要这个方法对IQueryable的运行,怎么把我的表拥有25万客户...

I need this method to run on the IQueryable, coz my table has 25 million clients...

我看到了关于SQL事件探查了蕴含在SQL转换...

I saw on the sql profiler that the Contains is transformed in sql...

我如何执行我的方法的相关滤波器代码发送到SQL? = /

How do i implement my method to send the correlative filter code to the sql? =/

推荐答案

一个简单的答案是您不能。在LINQ to SQL的翻译只能识别一些标准的.NET方法,也没有办法,你可以添加其他。

A simple answer is you can't. The LINQ to SQL translator recognizes only some of the standard .NET methods and there is no way you could add others.


  • 如果你需要类似的东西,你需要明确创建表达式树(用作lambda表达式的身体)。这样做的一个直接的方法是使用表达类的方法,但它可以simplfied不少。

  • If you need something like that, you'll need to create expression tree explicitly (to be used as the body of the lambda expression). A direct way of doing that is to use methods of the Expression class, but it can be simplfied a lot.

也许最好的办法是使用的 LINQKit项目。这使得它可以调用其他的 lambda表达式的(不太方法,但接近)。它提供了一个 AsExpandable 扩展方法,可以让你写的:

Perhaps the best option is to use the LINQKit project. It makes it possible to call other lambda expressions (not quite method, but close). It provides an AsExpandable extension method that allows you to write:

Expression<Func<Purchase, bool>> customFunction = ...
var data = new MyDataContext();
var query =
  from c in data.Purchases.AsExpandable()
  where customFunction.Compile()(c)
  select c.Name;



customFunction 是一个lambda函数编译成一个表达式树,你可以用它查询中。在 AsExpandable 扩展替换为函数体用,所以的LINQ to SQL翻译器可以处理这个问题。你可以阅读更多有关如何工作的我的博客文章

The customFunction is a lambda function compiled as an expression tree and you can use it inside a query. The AsExpandable extension replaces the use with the body of the function, so LINQ to SQL translator can handle this. You can read more about how this works in my blog post.

其他替代是由别人讨论更多的细节,是落实功能的SQL用户定义的函数。然后,你可以拖动功能,以您的数据上下文,并从查询调用该函数。译者将只需插入到您的SQL函数的调用。

Other alternative that is in more details discussed by others is to implement the functionality as an SQL user defined function. Then you can drag the function to your data context and call the function from the query. The translator will simply insert a call to your SQL function.

这篇关于如何创建询问服务转换为SQL的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆