Calling a user-defined function of a scalar database inside a LINQ to Entities query?

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

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

However, the instructions given here assume that you are using the DB-first approach and are talking about modifying the file .edmx. How about if you use a code-based approach? I want to write something like this:

var result = await (
    from itm in _itemDataContext.Items
    where
        itm.QryGroup1 == "Y" &&
        _itemDataContext.Dbo_MyCustomScalarIntFn(itm.QryGroup2) > 0
).ToArrayAsync();
+6
source share
2 answers

, Entity Framework, NuGet (CodeFirstFunctions),


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();
}
+3

, ExecuteSqlCommand, sql BD, :

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

, .

+1

All Articles