0

它很简单:我没有这个结果的模型/视图模型。我不想拥有它。我只想调用我的存储过程并创建一个动态类型。像这样的东西:

dynamic ceps = (from cep in context.Database.SqlQuery<dynamic>("spLogradouro @pCEP, @pUF, @pLOCALIDADE, @pTIPOLOGRADOURO, @pLOGRADOURO", new SqlParameter("pCEP", filter), new SqlParameter("pUF", ""), new SqlParameter("pLOCALIDADE", ""), new SqlParameter("pTIPOLOGRADOURO", ""), new SqlParameter("pLOGRADOURO", ""))
                              select new 
                        {
                            property_one = name_of_my_column_of_this_stored_procedure_return

                        }).Single();

不可能那么难。

编辑

安装我正在寻找的东西,但要实现我的目标:

  CepContext context = new CepContext();
        JsonResult result = Json(new { Street = "", District = "", City = "", UF = "" });
        using (var cmd = context.Database.Connection.CreateCommand())
        {

            cmd.CommandText = "spLogradouro @pCEP, @pUF, @pLOCALIDADE, @pTIPOLOGRADOURO, @pLOGRADOURO";
            cmd.Parameters.Add(new SqlParameter("pCEP", filter));
            cmd.Parameters.Add(new SqlParameter("pUF", ""));
            cmd.Parameters.Add(new SqlParameter("pLOCALIDADE", ""));
            cmd.Parameters.Add(new SqlParameter("pTIPOLOGRADOURO", ""));
            cmd.Parameters.Add(new SqlParameter("pLOGRADOURO", ""));

            cmd.Connection.Open();
            var reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                reader.Read();
                result = Json(new
                {
                    Street = reader.GetString(reader.GetOrdinal("log_logradouro")),
                    District = reader.GetString(reader.GetOrdinal("log_bairro")),
                    City = reader.GetString(reader.GetOrdinal("log_localidade")),
                    UF = reader.GetString(reader.GetOrdinal("log_uf"))
                });
            }
            cmd.Connection.Close();
        }
4

2 回答 2

0

不是我想要的,而是实现我的目标:

  CepContext context = new CepContext();
        JsonResult result = Json(new { Street = "", District = "", City = "", UF = "" });
        using (var cmd = context.Database.Connection.CreateCommand())
        {

            cmd.CommandText = "spLogradouro @pCEP, @pUF, @pLOCALIDADE, @pTIPOLOGRADOURO, @pLOGRADOURO";
            cmd.Parameters.Add(new SqlParameter("pCEP", filter));
            cmd.Parameters.Add(new SqlParameter("pUF", ""));
            cmd.Parameters.Add(new SqlParameter("pLOCALIDADE", ""));
            cmd.Parameters.Add(new SqlParameter("pTIPOLOGRADOURO", ""));
            cmd.Parameters.Add(new SqlParameter("pLOGRADOURO", ""));

            cmd.Connection.Open();
            var reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                reader.Read();
                result = Json(new
                {
                    Street = reader.GetString(reader.GetOrdinal("log_logradouro")),
                    District = reader.GetString(reader.GetOrdinal("log_bairro")),
                    City = reader.GetString(reader.GetOrdinal("log_localidade")),
                    UF = reader.GetString(reader.GetOrdinal("log_uf"))
                });
            }
            cmd.Connection.Close();
        }
于 2013-05-02T14:52:03.700 回答
0

您始终可以通过 C# 执行 SQL 并将结果集读入动态对象。

于 2013-04-30T16:26:28.883 回答