none
数据转换错误 RRS feed

  • 问题

  •  

    查询实体和关联(实体框架快速入门)

        private void CourseViewer_Load(object sender, EventArgs e)
            {
                //Initialize the ObjectContext
                schoolContext = new SchoolEntities1();

                // Define a query that returns all Department  
                // objects and course objects, ordered by name.
                var departmentQuery = from d in schoolContext.Departments.Include("Courses")  // Courses    Departments
                                      orderby d.Name
                                      select d;
                try
                {
                    // Bind the ComboBox control to the query, 
                    // which is executed during data binding.
                    // To prevent the query from being executed multiple times during binding, 
                    // it is recommended to bind controls to the result of the Execute method. 
                    this.departmentList.DisplayMember = "Name";
                    ObjectQuery bb = departmentQuery as ObjectQuery;
                    this.departmentList.DataSource = ((ObjectQuery)departmentQuery).Execute(MergeOption.AppendOnly);  

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

    无法将类型为“System.Data.Entity.Infrastructure.DbQuery`1[CourseManager.Department]”的对象强制转换为类型“System.Data.Entity.Core.Objects.ObjectQuery”。

    2016年10月10日 9:50

答案

  • departmentQuery结果集是一个IQuerable<T>的对象,无法和ObjectQuery相互转换。

    ObjectQuery一般是一个强类型的泛型集合,传入一个SQL语句,例如这样用法:

    using (var edm = new NorthwindEntities())
        {
                    string esql = "select value c.City from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
    
                  //返回一个string类型的类型,City为char相关类型
                    ObjectQuery<string> query = edm.CreateQuery<string>(esql);
                    query = query.Distinct();
                    foreach (string c in query)
                    {
                        Console.WriteLine("City {0}", c);
                    }
         }
    如果是返回是一个类型(一行记录),那么ObjectQuery应该返回一个类:
    using (var edm = new NorthwindEntities())
         {
                    string esql1 = "select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
                    ObjectQuery<Customers> query1 = edm.CreateQuery<Customers>(esql1);
                    string esql2 = "select value c from NorthwindEntities.Customers as c where c.Country='UK' order by c.CustomerID limit 10";
                    ObjectQuery<Customers> query2 = edm.CreateQuery<Customers>(esql2);
                    query1 = query1.Except(query2);
                    foreach (Customers c in query1)
                    {
                        Console.WriteLine(c.Country);
                        //输出:UK
                    }
            }

    ASP.NET Forum
    StackOverFlow
    FreeRice Donate
    Issues to report

    • 已编辑 ThankfulHeart 2016年10月10日 11:52
    • 已标记为答案 xukeqin 2016年10月11日 13:41
    2016年10月10日 11:48
  • 你好,

    我太清楚为什么要这样转换呢, 直接用ToList()方法试试。像这样。

    private void CourseViewer_Load(object sender, EventArgs e)
             {
                 //Initialize the ObjectContext
                 schoolContext = new SchoolEntities1();
    
                 // Define a query that returns all Department  
                 // objects and course objects, ordered by name.
                 var departmentQuery = (from d in schoolContext.Departments.Include("Courses")  // Courses    Departments
                                       orderby d.Name
                                       select d).ToList();
                 try
                 {
                     // Bind the ComboBox control to the query, 
                     // which is executed during data binding.
                     // To prevent the query from being executed multiple times during binding, 
                     // it is recommended to bind controls to the result of the Execute method. 
                     this.departmentList.DisplayMember = "Name";
                     //ObjectQuery bb = departmentQuery as ObjectQuery;
                     this.departmentList.DataSource = departmentQuery; //((ObjectQuery)departmentQuery).Execute(MergeOption.AppendOnly);  
    
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show(ex.Message);
                 }
             } 

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 xukeqin 2016年10月11日 13:41
    2016年10月11日 3:07
    版主

全部回复

  • departmentQuery结果集是一个IQuerable<T>的对象,无法和ObjectQuery相互转换。

    ObjectQuery一般是一个强类型的泛型集合,传入一个SQL语句,例如这样用法:

    using (var edm = new NorthwindEntities())
        {
                    string esql = "select value c.City from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
    
                  //返回一个string类型的类型,City为char相关类型
                    ObjectQuery<string> query = edm.CreateQuery<string>(esql);
                    query = query.Distinct();
                    foreach (string c in query)
                    {
                        Console.WriteLine("City {0}", c);
                    }
         }
    如果是返回是一个类型(一行记录),那么ObjectQuery应该返回一个类:
    using (var edm = new NorthwindEntities())
         {
                    string esql1 = "select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
                    ObjectQuery<Customers> query1 = edm.CreateQuery<Customers>(esql1);
                    string esql2 = "select value c from NorthwindEntities.Customers as c where c.Country='UK' order by c.CustomerID limit 10";
                    ObjectQuery<Customers> query2 = edm.CreateQuery<Customers>(esql2);
                    query1 = query1.Except(query2);
                    foreach (Customers c in query1)
                    {
                        Console.WriteLine(c.Country);
                        //输出:UK
                    }
            }

    ASP.NET Forum
    StackOverFlow
    FreeRice Donate
    Issues to report

    • 已编辑 ThankfulHeart 2016年10月10日 11:52
    • 已标记为答案 xukeqin 2016年10月11日 13:41
    2016年10月10日 11:48
  • 你好,

    我太清楚为什么要这样转换呢, 直接用ToList()方法试试。像这样。

    private void CourseViewer_Load(object sender, EventArgs e)
             {
                 //Initialize the ObjectContext
                 schoolContext = new SchoolEntities1();
    
                 // Define a query that returns all Department  
                 // objects and course objects, ordered by name.
                 var departmentQuery = (from d in schoolContext.Departments.Include("Courses")  // Courses    Departments
                                       orderby d.Name
                                       select d).ToList();
                 try
                 {
                     // Bind the ComboBox control to the query, 
                     // which is executed during data binding.
                     // To prevent the query from being executed multiple times during binding, 
                     // it is recommended to bind controls to the result of the Execute method. 
                     this.departmentList.DisplayMember = "Name";
                     //ObjectQuery bb = departmentQuery as ObjectQuery;
                     this.departmentList.DataSource = departmentQuery; //((ObjectQuery)departmentQuery).Execute(MergeOption.AppendOnly);  
    
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show(ex.Message);
                 }
             } 

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 xukeqin 2016年10月11日 13:41
    2016年10月11日 3:07
    版主
  • 这段代码是MSDN上的演练程序

    这是这段代码的源地址:  https://msdn.microsoft.com/zh-cn/library/bb386884.aspx


    2016年10月11日 6:46