locked
Cannot implicitly convert type System.Linq.IQueryable to System.Data.ObjectSet. An explicit conversion exists (are you missing a cast?) RRS feed

  • Question

  • I re-write my question. Maybe it will be easy for somebody to help me.

    BooksEntities be = new BooksEntities();

    using (var context = new BooksEntities())

    {

    var contextWhere = context.tblBooks;

    if (lbCategory.SelectedIndex != 0)

    {

    contextWhere = contextWhere.Where(x => x.TypeID == 5);

    }

    }

    It gives an error in the line   contextWhere = contextWhere.Where(x => x.TypeID == 5);

    Error: Cannot implicitly convert type System.Linq.IQueryable to System.Data.ObjectSet. An explicit conversion exists (are you missing a cast?)

    What is wrong?  Any suggestions?.

    Thank you.

    OkyToky


    It was previous edition of this question:

    Code below works, but lines 11-16 and 21-26 are the same.

    1       private void lbCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)

     2      {

     3           ocBook = new ObservableCollection<BookClass>();

     4           ocBook.Clear();

     5           BooksEntities be = new BooksEntities();

     6           using (var context = new BooksEntities())

     7           {               

     8               if (lbCategory.SelectedIndex == 0)

     9              {

    10               var contextWhere = context.tblBooks;

    11                foreach (var book in contextWhere)

    12                   {

    13                  int ID = book.ID;

    14                    string BookName = book.BookName;             

    15                    ocBook.Add(new BookClass(ID, BookName));

     16                   }

     17              }

    18                else

    19                {

     20                   var contextWhere = context.tblBooks.Where(x => x.TypeID == 5);

     21                  foreach (var book in contextWhere)

     22                  {

     23                       int ID = book.ID;

     24                       string BookName = book.BookName;

     25                       ocBook.Add(new BookClass(ID, BookName));

    26                    }

    27                }                    

    I want to re-write code to have one foreach block.

    1       private void lbCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)

     2      {

     3           ocBook = new ObservableCollection<BookClass>();

     4           ocBook.Clear();

     5           BooksEntities be = new BooksEntities();

     6           using (var context = new BooksEntities())

     7           {    

    8               var contextWhere = context.tblBooks;        

    9               if (lbCategory.SelectedIndex != 0)

    10              {

     11                 contextWhere = contextWhere.Where(x => x.TypeID == 5);

     12                  }

     13                  foreach (var book in contextWhere)

     14                  {

     15                       int ID = book.ID;

     16                       string BookName = book.BookName;

     17                       ocBook.Add(new BookClass(ID, BookName));

    18                    }

    19                }

    It gives an error on Line 11.

    Error: Cannot implicitly convert type System.Linq.IQueryable to System.Data.ObjectSet. An explicit conversion exists (are you missing a cast?)

    What is wrong?  Any suggestions are highly appreciated.

    Thank you.

    OkyToky


    Oak


    • Edited by OkyToky Tuesday, March 12, 2013 11:32 PM
    Monday, March 11, 2013 12:58 AM