简体   繁体   English

如何从EF LINQ查询调用DB函数?

[英]How to call DB function from EF LINQ query?

In my database I have define some function, let's say it's called fnTest . 在我的数据库中,我定义了一些函数,假设它叫做fnTest Is it possible to call that function from my LINQ/EF query? 是否可以从我的LINQ / EF查询中调用该函数? Something like this: 像这样的东西:

var param3, param4;
var res = (from x in db.Something
           where x.field1 > 0
           orderby fnTest(x.f1, x.f2, param3, param4)
           select x).Take(20);

As you can see, I need that function to execute on DB side because I need to sort data using value that it returns. 正如您所看到的,我需要在DB端执行该函数,因为我需要使用它返回的值对数据进行排序。 First two parameters are fields from the table, and second two parameters are some numbers that will change in program, but will be constant for each query. 前两个参数是表中的字段,后两个参数是一些将在程序中更改的数字,但对于每个查询将是常量。

Is it possible to somehow call function that is already created in the DB? 有可能以某种方式调用已在DB中创建的函数吗? Or do I need to use something like this: 或者我需要使用这样的东西:

((IObjectContextAdapter) context).ObjectContext.CreateQuery

and write the query manually? 并手动编写查询?

First off, fnTest will have to be created as a user-defined function in the database first: 首先, fnTest首先在fnTest中将fnTest创建为用户定义的函数:

CREATE FUNCTION [fnTest] (@fi int, @f2 int, @param3 int, @param4 int)
RETURNS int
AS ...

Then in your .edmx file, declare the function like this: 然后在.edmx文件中,声明如下函数:

<Function Name="fnTest" ReturnType="int" Schema="dbo" >
    <Parameter Name="f1" Mode="In" Type="int" />
    <Parameter Name="f2" Mode="In" Type="int" />
    <Parameter Name="param3" Mode="In" Type="int" />
    <Parameter Name="param4" Mode="In" Type="int" />
</Function>

Now you can bind this function to a method in your model like this: 现在,您可以将此函数绑定到模型中的方法,如下所示:

[EdmFunction("MyNamespace", "fnTest")]
public static int fnTest(int f1, int f2, int param3, int param4)
{
    throw new NotSupportedException("Direct calls are not supported.");
}

You can now use this method in standard LINQ queries. 您现在可以在标准LINQ查询中使用此方法。

Further Reading 进一步阅读

Use sql queries for entities. 对实体使用sql查询。 For exaples look on msdn: http://msdn.microsoft.com/en-us/data/jj592907.aspx 有关exaples的信息,请访问msdn: http//msdn.microsoft.com/en-us/data/jj592907.aspx

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

相关问题 如何从 EF Core 调用 DB2 表值 Function - How to Call DB2 Table Valued Function from EF Core 如何使用一次数据库往返执行两个EF linq查询 - How to execute two EF linq query with one db round trip EF 如何使用 DB 中 LINQ `Where` function 到 select 元素中提供的回调? - How does the EF use the callback provided in the LINQ `Where` function to select elements from DB? 如何使用EntityFramework在LINQ中调用DB函数 - How to call DB function in LINQ with EntityFramework 我如何使用linq和EF从SQL DB中进行选择,其中使用DateDiff函数从今天起到期的ExpiryDate为30天 - How can I select from a SQL DB using linq and EF where ExpiryDate is 30 days to expire from today using DateDiff function Linq to Sql-创建数据库以及如何从中查询? - Linq to Sql - Create DB and How to query from it? 在 LinQ 查询中调用函数 - Call Function In LinQ Query EF(LINQ to SQL)-如何查询? - EF (LINQ to SQL) - How to query? 带有表达式的 EF LINQ 查询:预期的方法名称 - 如何将表达式传递到要在 EF 查询中使用的函数中? - EF LINQ query with Expression: method name expected - How can I pass an Expression into function to be used in EF query? 如何从EF中的SingleOrDefault LINQ查询中选择列的子集 - How to Select subset of columns from SingleOrDefault LINQ query in EF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM