简体   繁体   English

为什么我不能在linq查询中调用函数?

[英]Why I can't call a function inside a linq query?

This is my linq query (Linq to Sql) : 这是我的linq查询(Linq to Sql):

pagina = (from Pagine page in kDataBase.Pagines
          where prova(page.title_it) == urlSuddivisa[i].ToLower()
          select page).FirstOrDefault();

and this is the function I call (inside the same class) : 这是我调用的函数(在同一类中):

private string prova(string example)
{
    return example;
}             

it says : 它说 :

there are not supported conversion to sql for the method 'System.String prova(System.String). 方法'System.String prova(System.String)不支持转换为sql。

Where am I wrong? 我哪里错了? And how can I fix it? 我该如何解决?

Since the linq query is converted into a SQL query that runs against your dB it is not able to translate your custom function into a SQL query. 由于linq查询被转换为针对您的dB运行的SQL查询,因此无法将您的自定义函数转换为SQL查询。

Alternative ways of appraoaching the problem 解决问题的替代方法

  • Use a stored procedure and read about using stored procedure in linq 使用存储过程,并阅读有关在linq中使用存储过程的信息
  • retrieve your data from SQL to memory and then filter it based on your function. 从SQL检索数据到内存,然后根据您的功能对其进行过滤。 The disadvantage obviously here is you will be retrieving a lot many number of rows from the database than what is required 显然,这里的缺点是您将从数据库中检索许多行,而不是所需的行。

    var paginas=(from Pagine page in kDataBase.Pagines).ToList().Where(p =>prova(p.title_it) == urlSuddivisa[i].ToLower()).FirstOrDefault();

    is what you want. 是你想要的。

On a side note I am guessing your method prova does more than just returning the string otherwise the function is outright useless and you may as well get rid of the function. 从侧面讲,我猜测您的方法prova不仅会返回字符串,否则该函数将完全无用,并且您也可能会摆脱该函数。 Also I believe you are firing multiple queries to the database cos of the array that you are using with a counter i you could always use the IN query and get around it, remember the restriction of the number of element in an IN query. 此外,我相信你是射击多个查询到数组的数据库COS,你使用的是有一个计数器i ,你总是可以使用IN查询,并围绕它得到的,记得元素的数量限制在IN查询。

Your linq query expression is translated into sql at runtime, and the framework doesn't know how to convert the prova method call into an appropriate sql. 您的linq查询表达式在运行时转换为sql,并且框架不知道如何将prova方法调用转换为适当的sql。 Instead, you can do something like this: 相反,您可以执行以下操作:

pagina = (from Pagine page in kDataBase.Pagines select page).ToList().Where(p => prova(p.title_it) == urlSuddivisa[i].ToLower()).FirstOrDefault();

This will pull the pages into memory first via the toList, where the prova method can then be evaluated. 这将首先通过toList将页面拉入内存,然后可以在其中评估prova方法。

With linq to sql the Expression tree get parsed to SQL query instead of just running it like it is with linq to objects . 使用linq to sqlExpression tree将解析为SQL查询,而不是像linq to objects一样运行它。

Your ORM provider doesn't know hot to convert the method to SQL. 您的ORM提供程序不知道将方法转换为SQL的热点。

You shouldn't use methods in linq to SQL LINQ queries. 您不应该在linq to SQL使用方法linq to SQL LINQ查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM