Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a custom scalar DB function inside a LINQ to Entities query?

Is there a way I can call a custom scalar DB function as part of my LINQ to Entities query? The only thing I can find on the web about this is this page:

https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/ef/language-reference/how-to-call-custom-database-functions

However the instructions given here seem to assume you are using a DB-first approach and talk about modifying the .edmx file. What about if you're using a code-first approach? I want to be able to write something like this:

var result = await (
    from itm in _itemDataContext.Items
    where
        itm.QryGroup1 == "Y" &&
        _itemDataContext.Dbo_MyCustomScalarIntFn(itm.QryGroup2) > 0
).ToArrayAsync();
like image 447
Jez Avatar asked Mar 02 '18 18:03

Jez


People also ask

Can I use LINQ with Entity Framework?

Entity Framework Core uses Language-Integrated Query (LINQ) to query data from the database. LINQ allows you to use C# (or your . NET language of choice) to write strongly typed queries.

Which entities can LINQ use to perform queries?

LINQ to Entities queries are comprised of LINQ standard query operators (such as Select, Where, and GroupBy) and expressions (x > 10, Contact. LastName, and so on). LINQ operators are not defined by a class, but rather are methods on a class.

How LINQ query execute?

LINQ queries are always executed when the query variable is iterated over, not when the query variable is created. This is called deferred execution. You can also force a query to execute immediately, which is useful for caching query results. This is described later in this topic.

Is LINQ a query builder?

dynamic-linq-query-builder is a small library that allows any . Net framework class collection to be filtered dynamically at runtime.

What is call function in LINQ?

Calling functions in LINQ queries. The LINQ project (see [1]) is an extension to .NET Framework and most common .NET languages (C# and VB.Net) that extends these languages with query operators and some other features that make it possible to integrate queries in the languages.

How to call a function from a database in Entity Framework?

The first step is to create a static class as a mechanism to call the database function. We then need to register our method and its SQL translation with Entity Framework Core. There are few notable parts to this registration: EF Core uses the MethodInfo to determine if the translation can continue.

How do I call a custom database function in SQL Server?

The steps for calling a custom database function are the same regardless of the database server. However, the code below is specific to creating a function in a SQL Server database. The code for creating a custom function in other database servers might differ. USE [School] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE FUNCTION [dbo].

How to add a function attribute to a class in LINQ?

Add a corresponding method to a class in your application code and apply a EdmFunctionAttribute to the method Note that the NamespaceName and FunctionName parameters of the attribute are the namespace name of the conceptual model and the function name in the conceptual model respectively. Function name resolution for LINQ is case sensitive.


Video Answer


2 Answers

Turns out this functionality is not built in to Entity Framework so I had to use a NuGet package (CodeFirstFunctions) to get it:


IItemDataContext

[DbFunction("CodeFirstDatabaseSchema", "fn_IsCorrectProduct")]
bool Fn_IsCorrectProduct(string companyID, string itemCode);

ItemDataContext

[DbFunction("CodeFirstDatabaseSchema", "fn_IsCorrectProduct")]
public bool Fn_IsCorrectProduct(string companyID, string itemCode)
{
    // UDF is described in DbFunction attribute; no need to provide an implementation...
    throw new NotSupportedException();
}
like image 102
Jez Avatar answered Oct 18 '22 02:10

Jez


I think you can use ExecuteSqlCommand, but you have to hardcode the sql to be executed against BD, like this:

var result = await (
    from itm in _itemDataContext.Items
    where
        itm.QryGroup1 == "Y" &&
        _itemDataContext.Database.ExecuteSqlCommand("Exec yourFunction "+itm.QryGroup2) > 0
).ToArrayAsync();

I dont tested, but should solve your problem.

like image 38
Rui Estreito Avatar answered Oct 18 '22 03:10

Rui Estreito