Solved

How do I use Database.SqlQuery<no class>  without a known class?

Posted on 2014-07-29
7
4,810 Views
Last Modified: 2014-08-13
In this Stored Procedure call:
string color = "Red";
SqlParameter colorParam = new SqlParameter(@color, color);
var redTractors = context.Database.SqlQuery<Tractor>("usp_GetTractors", @color, colorParam).ToList();
everything works fine because you know the results are of the Tractor class,
BUT,
what if you don't know the return type/class of the stored procedure, like in the case of a PIVOT table where the column names in the result are not known ahead of time?
string propertyId = 62;
SqlParameter propIdParam = new SqlParameter(@propertyId, propertyId);
var summary = context.Database.SqlQuery<?????>("usp_GetSummaryData", @propertyId, propIdParam).ToList();
0
Comment
Question by:jamestieman
  • 4
  • 3
7 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40226856
Hi jamestieman;

Create a class which will hold a single row from the result set. The names of the properties of the class must match the columns names, spelling and capitalization, of the results from the query. In the sample blow I created a class called SummaryResults with three public properties. These properties match what is expected from the query. That should give you what you need.
string propertyId = "62";
SqlParameter propIdParam = new SqlParameter("@propertyId", propertyId);
var summary = context.Database.SqlQuery<SummaryResults>("usp_GetSummaryData @propertyId", new object[] {propIdParam}).ToList();

public class SummaryResults
{
	public string Name { get; set; }
	public int Total { get; set; }
	public string Department { get; set; }
}

Open in new window

0
 

Author Comment

by:jamestieman
ID: 40227263
The problem is that the PIVOT is on the Description column and descriptions can be added, edited and deleted.  Also, different properties may have different descriptions.  Some properties will have up to 200 different Account Descriptions.  The first part of the PIVOT shows how I get all the Descriptions necessary for the PIVOT clause.
 
The only thing that makes sense to me is to query for the all the different descriptions, like the first part of the PIVOT, create a dynamic class on the fly, then call the stored procedure using the created class.  
My ultimate objective is to create a CSV file out of the data and email it to specific users.
If I could have SQL Server return a CSV file, then I wouldn't need the class at all.

Here is the PIVOT:
CREATE PROCEDURE [dbo].[usp_DynamicSummaryPivot]
      -- Add the parameters for the stored procedure here
      @masterPropertyId int = 60,
      @propertyId int = 62,
      @year int = 2014,
      @month int = 6
AS
BEGIN

DECLARE @columns NVARCHAR(MAX);
DECLARE @sql NVARCHAR(MAX);
SET @columns = N'';

SELECT @columns += N', p.' + QUOTENAME(Description)
      FROM (SELECT mdl.Description
FROM         TransactionHeaders th INNER JOIN
                      TransactionDetails td ON th.Id = td.TransactionHeaderId INNER JOIN
                      PropertyDataCollectors pdc ON td.PropertyDataCollectorId = pdc.Id INNER JOIN
                      MasterDataCollectors mdc ON pdc.MasterDataCollectorId = mdc.Id INNER JOIN
                      MasterDetailLines mdl ON mdc.MasterDetailLineId = mdl.Id
WHERE     (th.PropertyId = @propertyId) AND (mdl.MasterPropertyId = @masterPropertyId)
Group by mdl.Description) AS x;

SET @sql = N'
SELECT [Property], [Date], ' + STUFF(@columns, 1, 2, '') + '
FROM
(
  SELECT Properties.Name as Property, CONVERT(VARCHAR(10), [TransactionHeaders].[TransactionDate], 101) as [Date], MasterDetailLines.Description AS MDLDesc,
                      ISNULL(TransactionDetails.Amount, 0) AS Total
FROM         TransactionHeaders INNER JOIN
                      Properties ON TransactionHeaders.PropertyId = Properties.Id INNER JOIN
                      TransactionDetails ON TransactionHeaders.Id = TransactionDetails.TransactionHeaderId INNER JOIN
                      PropertyDataCollectors ON TransactionDetails.PropertyDataCollectorId = PropertyDataCollectors.Id INNER JOIN
                      MasterDataCollectors ON PropertyDataCollectors.MasterDataCollectorId = MasterDataCollectors.Id INNER JOIN
                      MasterDetailLines ON MasterDataCollectors.MasterDetailLineId = MasterDetailLines.Id
WHERE TransactionHeaders.PropertyId = ' + STR(@propertyId) + '
AND YEAR(TransactionHeaders.TransactionDate) = ' + STR(@year) + '
 AND MONTH(TransactionHeaders.TransactionDate) = ' + STR(@month) + '
                      ) as t
PIVOT
(
  SUM(Total) FOR MDLDesc IN ('
  + STUFF(REPLACE(@columns, ', p.[', ',['), 1, 1, '')
  + ')
) AS p;';
--PRINT @propertyId;
--PRINT @sql;
EXEC sp_executesql @sql;
END


GO
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40227284
Are you saying that the stored procedure is not always or will always return the same columns from the database?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:jamestieman
ID: 40227315
It will probably never return the same columns because the description of different accounts will be different between properties.  Also, some properties will have more accounts than others.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40227425
Please try the following. Open the EDMX designer and in the Model Browser window expand the Complex Type node. See if you have a complex type called something like usp_GetSummaryData_Result. Use that as the type of entities to be returned, something like the following:
var summary = context.Database.SqlQuery<usp_GetSummaryData_Result>("usp_GetSummaryData @propertyId", new object[] {propIdParam}).ToList();

Open in new window

EF should have analyzed the store procedure and determined the result set.
0
 

Author Comment

by:jamestieman
ID: 40227524
There's nothing in the Complex Types, so I right-clicked the SP and clicked Add Function Import.  When I click Get Column Information, I get "The selected stored procedure returns no columns.".
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40227784
Well then you are not going to be able to execute that stored procedure using Entity Framework because all the methods that allow you to do that requires a known type to be return. You will need to use ADO .Net  and possibly use a data reader to return the values and build your CSV file from the reader.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question