EF 6.1 select scalar function result

c# entity-framework entity-framework-6 sql-server

Question

How to select database scalar function result as column using Entity Framework 6.1 Code First? This is pure SQL query:

SELECT
Id AS Id
, Name AS Name
, dbo.GetAdditionalInfo(Id) AS AdditionalInfo
FROM Users
WHERE Status = 1

Database function dbo.GetAdditionalInfo(INT Id) collects info from other tables and returns VARCHAR result. How to do that using EF 6.1? I need something like this:

var usersList = ctx.Users
.Where(n=>n.Status == 1)
.Select(n=>new{
    Id = n.Id
    , Name = n.Name
    , AdditionalInfo = ctx.DataBase.GetScalarFunctionResult<string>("dbo.GetAdditionalInfo (@)", n.Id)
}).ToList();

Of course,

ctx.DataBase.GetScalarFunctionResult<T>(string ScalarFunctionName, params object args)

doesn't exists and that's not working. But imho it could be very useful and easy to use, if implemented.

1
1
9/8/2016 9:48:15 AM

Popular Answer

It seems, that what you want to achieve is described here Entity Framework 6 Code First Custom Functions. It will require an additional library, because it seems that EF6 does not support calling UDFs with Code-first out-of-the-box (Calling a SQL User-defined function in a LINQ query).

1
5/23/2017 11:54:11 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow