Click here to Skip to main content
15,887,923 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi Team,
Here I got issue while fetching data from dynamic created procedure in linq.
it returns 0

Procedure is created with the help of Pivot data from existing table.

Here is Stored Procedure.

SQL
ALTER proc [dbo].[usp_tblProperty]
AS
DECLARE @PivotColumnHeaders VARCHAR(MAX)
SELECT @PivotColumnHeaders =
  COALESCE(
    @PivotColumnHeaders + ',[' + cast(propertyName as varchar(55)) + ']',
    '[' + cast(propertyName as varchar(55))+ ']'
  )
FROM tblproperty
print @PivotColumnHeaders
DECLARE @PivotTableSQL NVARCHAR(MAX)
SET @PivotTableSQL = N'
  SELECT *
  FROM (
    SELECT
     EntityObjectId,tblorganisationentity.OrganisationName,tblproperty.propertyname as propertyname,value
    FROM tblpropertyvalue inner join tblProperty
    on tblpropertyvalue.propertyid = tblproperty.propertyid
    inner join tblorganisationentity 
    on tblorganisationentity.organisationId = tblpropertyvalue.entityobjectid
   
  ) AS PivotData
  PIVOT (
    max(value)
    FOR propertyname IN (
      ' + @PivotColumnHeaders + '
    )
  ) AS PivotTable
'
Print @PivotTableSQL
EXECUTE(@PivotTableSQL)


OutPut of Procedure is something like below mention data:

OrganisationId OrganisationName SAdd1 lSubscriberId

F649AD83-CA02-43AC-9A5E-FD6B183415F1 3M Norge A.S 0 3M




In C# Page when i try to access data with linq it return integer value and i am unable to access proerties from procedure.

Here Code in C# :
C#
var elno = objEfoDataContext.usp_tblProperty();
elno return 0
Posted
Updated 13-Apr-18 12:35pm
v3

Its Solution is that you can create view of inner query.

DECLARE @PivotColumnHeaders VARCHAR(MAX)
SELECT @PivotColumnHeaders =
COALESCE(
@PivotColumnHeaders + ',[' + cast(propertyName as varchar(55)) + ']',
'[' + cast(propertyName as varchar(55))+ ']'
)
FROM tblproperty
print @PivotColumnHeaders
DECLARE @PivotTableSQL NVARCHAR(MAX)
SET @PivotTableSQL = N'
create view abc
as

SELECT *
FROM (
SELECT
EntityObjectId,tblorganisationentity.OrganisationName,tblproperty.propertyname as propertyname,value
FROM tblpropertyvalue inner join tblProperty
on tblpropertyvalue.propertyid = tblproperty.propertyid
inner join tblorganisationentity
on tblorganisationentity.organisationId = tblpropertyvalue.entityobjectid

) AS PivotData
PIVOT (
max(value)
FOR propertyname IN (
' + @PivotColumnHeaders + '
)
) AS PivotTable
'

Now you can access view as
"select * from abc"
 
Share this answer
 
v2
Hi,

To read the results of a stored procedure in Linq you need to create a list for the proc to populate:

C#
using(objEfoDataContext dc = new objEfoDataContext())
{

List<usp_tblproperty> myresult = new List<usp_tblproperty>();
myresult= dc.usp_tblPropertyResult().tolist();
//myresult will have the data from your stored procedure
}</usp_tblproperty></usp_tblproperty>
 
Share this answer
 
hi. here is solved your problem..


DataClasses2DataContext db = new DataClasses2DataContext();

        IEnumerable<sp_SonYapilanlarResult> sp_gelen = (IEnumerable<sp_SonYapilanlarResult>)db.sp_SonYapilanlar();

        List<sp_SonYapilanlarResult> listemeARTI = new List<sp_SonYapilanlarResult>();
        List<sp_SonYapilanlarResult> listemeSIFIR = new List<sp_SonYapilanlarResult>();
        List<sp_SonYapilanlarResult> listemeEKSI = new List<sp_SonYapilanlarResult>();
        foreach (sp_SonYapilanlarResult gelenItem in sp_gelen)
        {
            if (gelenItem.Delta > 0)                listemeARTI.Add(gelenItem);
            if (gelenItem.Delta == 0)                listemeSIFIR.Add(gelenItem);
            if (gelenItem.Delta < 0)                listemeEKSI.Add(gelenItem);
        }

        GridView1.DataSource = listemeARTI;
        GridView1.DataBind();

        GridView2.DataSource = listemeSIFIR;
        GridView2.DataBind();

        GridView3.DataSource = listemeEKSI;
        GridView3.DataBind();
 
Share this answer
 
Comments
Maciej Los 14-Apr-18 14:14pm    
6 years too late ;(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900