Solved

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

Posted on 2014-07-29
7
4,453 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 62

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 62

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 62

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 62

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now