I have a View that typically gets query results from a WebMatrix Query (IEnumerable<dynamic>
data type), and displays the results in a table:
@model MySite.Models.Entity
@foreach(var row in Model.Data)
{
<tr>
@foreach (var column in row.Columns)
{
<td>@column<span>:</span> @row[column]</td>
}
</tr>
}
Here's my model where I query the database:
public class Entity
{
public dynamic Data {get; set; }
public Entity(String table)
{
if (table == "User" || table == "Group)
{
WebMatrix.Data.Database db = new WebMatrix.Data.Database();
db.Open(ConString);
Data = db.Query("SELECT * FROM " + table);
}
else
{
using (OdbcConnection con = ne4w OdbcConnection(ConString))
{
OdbcCommand com = new OdbcCommand("Select * From " + table);
command.CommandType = System.Data.CommandType.Text;
connection.Open();
OdbcDataReader reader = command.ExecuteReader();
Here's all the different things I've tried from reading various other posts:
// Atempt 1
Data = reader;
// Error in view, 'Invalid attempt to call FieldCount when reader is closed' (on 'var row `in` Model.Data')
// Atempt 2
Data = reader.Cast<dynamic>;
// Error: 'Cannot convert method group "Cast" to non-delegate type "dynamic". Did you intend to invoke the method?
// Atempt 3
Data = reader.Cast<IEnumerable<dynamic>>;
// Error same as Atempt 2
// Atempt 4
Data = reader.Cast<IEnumerable<string>>;
// Error same as Atempt 2
}
}
}
}
I'm looking for the best way to get the reader object to a IEnumerable<dynamic>
object.
Please note this is a simplified example, and while the reason for the two query types is not obvious, they are necessary in my code.