具有未知类型的EF ExecuteStoreQuery

时间:2014-01-22 10:17:52

标签: c# linq entity-framework

我正在尝试执行下面的代码,当我将type作为对象时它会正确执行,但我不知道如何从结果中读取数据。还有其他方法吗?

string sql = "select * from sys.columns where Name = N'@columnName' and Object_ID = Object_ID(N'@tableName')";
var args = new DbParameter[] { new SqlParameter { ParameterName = "columnName", Value = "Name2" }, new SqlParameter { ParameterName = "tableName", Value = "OrderDetails" } };
var students = ctx.ExecuteStoreQuery<object>(sql, args);

2 个答案:

答案 0 :(得分:0)

如果你有一个可以保存查询结果的具体类型,我认为你可以这样做:

List<Student> students = new List<Student>(); 
students = ctx.ExecuteStoreQuery<student>(sql, args);

然后遍历列表

foreach (Student student in students)
{
        Console.WriteLine(student.Name); //or whatever
}

答案 1 :(得分:0)

这是我提出的解决方案

string sql = "select * from sys.columns where Name = @columnName and Object_ID = Object_ID(@tableName)";

DataSet ds= new DataSet();
EntityConnection entityConn = (EntityConnection)ctx.Connection;
SqlConnection sqlConn = (SqlConnection)entityConn.StoreConnection;
SqlCommand cmd = new SqlCommand(sql, sqlConn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
using (cmd)
{
       SqlParameter Prm = new SqlParameter("tableName", "OrderDetails");
       cmd.CommandType = CommandType.Text;
       cmd.Parameters.Add(Prm);
       da.Fill(ds);
 }