Query with Scalar Result

This topic demonstrates creating a method in Entity Developer NHibernate model, which will execute a parameterized query returning the number of table records that satisfy the query condition.

For this, perform the following steps:

1.In the Model Explorer context menu select the Add submenu and then select New Method, or click the corresponding button on the Model Explorer toolbar or on the main application toolbar.
2.In the Method Editor dialog specify the SQL for the method, set the return type and customize the parameters:

images_QueryWithScalarResultGeneral

3.Customize the parameters:

images_QueryWithScalarResultParameters

4.Click OK.

The method is created.

As a result of the code generation for the model, the following method is generated:

C#:

public static System.Nullable<int> GetNumberOfDepartmentsByLocation(NHibernate.ISession session, string Loc)
        {
            NHibernate.IQuery query = session.GetNamedQuery(@"GetNumberOfDepartmentsByLocation");
            query.SetParameter(@"Loc", Loc);
            return ((System.Nullable<int>)(query.UniqueResult()));
        }

Visual Basic:

Public Shared Function GetNumberOfDepartmentsByLocation ( _
    session As NHibernate.ISession, Loc As String) As System.Nullable(Of Integer)
            Dim query As NHibernate.IQuery = session.GetNamedQuery("GetNumberOfDepartmentsByLocation")
            query.SetParameter("Loc", Loc)
            Return CType(query.UniqueResult(), System.Nullable(Of Integer))
        End Function

note Note

The SQL query must be built in such a way, so that the scalar value it returns, would be returned as a column with the name 'return_value', i.e. the return value must have the 'return_value' alias.

Now it is possible to use this query in the application with the help of method wrapper. This allows working with the query with all possible convenience, as wrapper methods are strongly typed, are found by IntelliSense and have the corresponding signature.

 

ExpandedToggleIcon        See Also


Send feedback on this topic

© 2008 - 2024 Devart. All rights reserved.