none
Type Cast Error RRS feed

  • Question

  • Hi,

       I am newibe in Enity Framework . I got the error like " Cannot implicitly convert type 'System.Linq.IOrderedQueryable<EFDemo.Department>' to 'System.Data.Objects.ObjectQuery<EFDemo.Department>'. An explicit conversion exists (are you missing a cast?) D:\EFTrial\EFDemo\EFDemo\Form1.cs 25 122 EFDemo" . If i wrote the Code,

    ObjectQuery

    <Department> departmentQuery = from d in schoolContext.Departments.Include("Courses") orderby d.Name

                                                           select d;

    What will i do?.

    Regards

    Dileep

    Tuesday, February 21, 2012 10:44 AM

Answers

  • var departmentQuery = from d in schoolContext.Departments.Include("Courses")

       order by d.Name

       select d;

    then do what you want with departmentQuery like turn it into a list departmentQuery.toList

    foreach(var dQ in departmentQuery.toList())

    {

    }

    better still

    tableDepartments dept = new tableDepartments(); //whatever your table name is

    dept = schoolContext.tableDepartments.Include("Courses");

    foreach(var d in dept)

    {

    //do something

    }


    • Edited by buthfa Tuesday, February 21, 2012 1:54 PM
    • Marked as answer by Allen_MSDNModerator Friday, February 24, 2012 12:56 AM
    Tuesday, February 21, 2012 1:54 PM
  • Hi Dileep;

    Not all Linq queries return the same data type. In this case the last method before the select is orderby which returns objects of type IOrderedQueryable<T> and because the select method does not change the shape of the results it will return objects of type IOrderedQueryable<Department> and so the error message is trying to say that it does not know how to convert from IOrderedQueryable<Department> to ObjectQuery<Department>. So you can do the following:

    IOrderedQueryable<Department> departmentQuery = 
        from d in schoolContext.Departments.Include("Courses") 
        orderby d.Name
        select d;

    Or do as @buthfa stated in his post by using var.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, February 21, 2012 2:55 PM

All replies

  • var departmentQuery = from d in schoolContext.Departments.Include("Courses")

       order by d.Name

       select d;

    then do what you want with departmentQuery like turn it into a list departmentQuery.toList

    foreach(var dQ in departmentQuery.toList())

    {

    }

    better still

    tableDepartments dept = new tableDepartments(); //whatever your table name is

    dept = schoolContext.tableDepartments.Include("Courses");

    foreach(var d in dept)

    {

    //do something

    }


    • Edited by buthfa Tuesday, February 21, 2012 1:54 PM
    • Marked as answer by Allen_MSDNModerator Friday, February 24, 2012 12:56 AM
    Tuesday, February 21, 2012 1:54 PM
  • Hi Dileep;

    Not all Linq queries return the same data type. In this case the last method before the select is orderby which returns objects of type IOrderedQueryable<T> and because the select method does not change the shape of the results it will return objects of type IOrderedQueryable<Department> and so the error message is trying to say that it does not know how to convert from IOrderedQueryable<Department> to ObjectQuery<Department>. So you can do the following:

    IOrderedQueryable<Department> departmentQuery = 
        from d in schoolContext.Departments.Include("Courses") 
        orderby d.Name
        select d;

    Or do as @buthfa stated in his post by using var.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, February 21, 2012 2:55 PM