Reputation: 312
Is there any way to query SQL in a Razor / Webmatrix page to get a specific (distinct) record from a specified column in a single row without using foreach?
I know how to setup the connection and get data out using foreach but is there a way to get a single record from a specific column without a loop?
Issue I have is that I am not running a SELECT query. I am executing a stored procedure which does the SELECT within it. There is one column that has a specific identifier which is the same for all returned records based on the filter. I am trying to get just 1 instance of this value so that I can display it in my page header.
If I was using a SELECT statement I wouldn't have an issue. I would just get the distinct value from the column I need but with a stored procedure I can't specify columns, distinct, etc...I can only pass parameters.
Is there a way to get just one distinct value instead of all values from a column without select distinct?
Upvotes: 0
Views: 1173
Reputation: 312
Found the solution to what I was trying to do which is to get a record out of a specific row and column. Used this:
var db = Database.Open("MyDb");
var sql = @"EXEC myProcedure";
var result = db.Query(sql);
var header_value = result.ElementAt(0);
.
.
.
@header_value.ColumnName
Upvotes: 1