Asked by:
Get scalar value from stored proc/ function import

Question
-
I am trying to figure how to get the value, when i add a watch on res i see there is a value of 8 in the [0] index but...
AdventureWorksEntities1 aw = new AdventureWorksEntities1();
int? i=312;
ObjectResult<Int32?> res = aw.CountOrders(i);
var v = res.Cast<Int32>();
// var v2 = v.FirstOrDefault();
Console.WriteLine( v.ToString());
Console.ReadLine();
M~
Monday, July 2, 2012 10:44 PM
All replies
-
Hi WIJ,
Please refer to this good example http://www.devtoolshed.com/using-stored-procedures-entity-framework-scalar-return-values
- Marked as answer by Allen_MSDN Monday, July 9, 2012 2:28 AM
- Unmarked as answer by WIJ Monday, July 9, 2012 12:27 PM
Tuesday, July 3, 2012 9:25 AM -
This didnt help the output is still -1, here is all my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UsingSPDemo
{
public static class Class1
{
public static void Main()
{
int? i = 312;
AdventureWorksEntities ent = new AdventureWorksEntities();
System.Nullable<int> c = ent.CountOrders(i);
var c2 = ent.CountOrders(312);
if (c.HasValue)
Console.WriteLine(c.Value);
Console.ReadLine();
}
}
}
<Schema Namespace="AdventureWorksModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
<EntityContainer Name="AdventureWorksModelStoreContainer" />
<Function Name="uspCountOrders" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="Sales">
<Parameter Name="ContactID" Type="int" Mode="In" />
</Function>
</Schema></edmx:StorageModels>
SET QUOTED_IDENTIFIER OFF;
SET ANSI_NULLS ON;
GO
USE [AdventureWorks]
GO
-- --------------------------------------------------
-- Creating stored procedure 'uspCOuntOrders'
-- --------------------------------------------------
alter PROCEDURE [Sales].[uspCountOrders]
@ContactID [int] = 1
AS
BEGIN
Set Nocount on
SELECT COUNT(*)
FROM [Sales].[SalesOrderHeader]
WHERE [ContactID] = @ContactID
END;
GO
M~
Monday, July 9, 2012 12:31 PM -
Hi WIJ,
I suggest you to check whether execute the stored procedure in the SQL Server Management Studio will return the expected value.
Best Regards
Allen Li [MSFT]
MSDN Community Support | Feedback to us
Tuesday, July 24, 2012 6:05 AM