none
从具体化“System.Data.Objects.MaterializedDataRecord”类型到“DBUtility.ColumnDatas”类型的指定强制转换无效。 RRS feed

  • 问题

  • 从具体化“System.Data.Objects.MaterializedDataRecord”类型到“DBUtility.ColumnDatas”类型的指定强制转换无效。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Data.Objects;


    namespace myMvcApp.Controllers
    {
        //using myMvcApp.Module;
        using DBUtility;

        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                ViewBag.Message = "欢迎使用 ASP.NET MVC!";

                DbEntities ent = new DbEntities();
                UserTables cd = new UserTables();
                cd.tbName = "fsf";
                cd.usr_id = 2;
                ent.UserTables.AddObject(cd);
                ent.SaveChanges();

                string esql = "select c.id from DbEntities.columndatas as c";
                ObjectQuery<ColumnDatas> q = ent.CreateQuery<ColumnDatas>(esql);

                ObjectResult<ColumnDatas> r = q.Execute(MergeOption.NoTracking);


                foreach (ColumnDatas e in r)
                {
                    int i = e.id;
                    int ii = e.col_id;
                }

                return View();
            }

        }
    }

    一到加粗得那段代码就报出这样异常。


    2012年1月6日 6:59

全部回复

  • Hi Neo,

    你在你的esql中犯了2个小小的错误:

    >> string esql = "select c.id from DbEntities.columndatas as c";

    这边只有Id的字段被select出来了,所以你的foreach肯定不能用这个完整类型来接受, 第二个是要用value关键字

    你试试改成:  string esql = "select value c from DbEntities.columndatas as c";

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2012年1月6日 8:47
    版主