Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ef Core 5.0] Call a scalar function and retrieve value #21057

Closed
julienGrd opened this issue May 27, 2020 · 5 comments
Closed

[Ef Core 5.0] Call a scalar function and retrieve value #21057

julienGrd opened this issue May 27, 2020 · 5 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@julienGrd
Copy link

This issue follow this one #21039, but focus on the call on the scalar functions

On the same way, how I can call a scalar function directly? I only find example when its use inside entity request but not from outside ? (with the DbFunction attribute)

If i take back my example

CREATE FUNCTION [dbo].[FnGetNbMessages](@DestId int, @EtsId tinyint, @Statut tinyint)
RETURNS int
AS
BEGIN
	return ISNULL(
	(
		SELECT	Count(Id) 
		FROM	[Messages] Msg
					LEFT JOIN GrpDests_Users Dg on DG.GrpNum = Msg.DestGrpNum AND DG.UserID = @DestId
		WHERE	
				--DestUserID = @DestId AND (EtsID IS NULL OR EtsID = @EtsId) AND (@Statut IS NULL OR Statut = @Statut)
				(@EtsId IS NULL OR Msg.EtsID is NULL OR Msg.EtsID = @EtsId)
					AND (DestUserID = @DestId OR DG.UserID IS NOT NULL)
					AND (@Statut IS NULL OR Statut = @Statut)
	), 0)

END;

I try

var result = this.Set<IntReturn>().FromSqlRaw("select dbo.FnGetNbMessages(@p0, @p1, @p2)", destinataireId, etsId, statut)
                        .AsEnumerable().FirstOrDefault()?.Nb ?? 0;

but without success (and it seem i can't put "as Nb" in the scalar function)

thanks !

@ErikEJ
Copy link
Contributor

ErikEJ commented May 27, 2020

What does "without success" mean? An error or no result?

@julienGrd
Copy link
Author

I have this exception
System.InvalidOperationException 
The required column 'Nb' was not present in the results of a 'FromSql' operation.'

@smitpatel
Copy link
Member

#9810

@ErikEJ
Copy link
Contributor

ErikEJ commented May 27, 2020

Have you tried:

select dbo.FnGetNbMessages(@p0, @p1, @p2) AS Nb

@julienGrd
Copy link
Author

Oh yes you have right i don't think about that, it works perfectly, thanks !

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label May 27, 2020
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

4 participants