Solved

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

Posted on 2014-07-29
7
5,183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

737 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