Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot implicitly convert type System.Data.Entity.DbSet<DataGridSQLExample.Product> to System.Data.Objects.ObjectQuery<DataGridSQLExample.Product>

I am using Visual Studio 2012 C Sharp (C#) and SQL Server Express. I am trying to go through the MSDN Walkthrough: Display Data from a SQL Server Database in a DataGrid Control. I have added the recommended AdventureWorksLT2008 sample database. Here are the instructions I followed:

  1. Create a new WPF Application project in Visual Basic or C#, and name it DataGridSQLExample.
  2. In Solution Explorer, right-click your project, point to Add, and then select New Item. ( The Add New Item dialog box appears. )
  3. In the Installed Templates pane, select Data and in the list of templates, select ADO.NET Entity Data Model.
  4. Name the file AdventureWorksModel.edmx and then click Add. (The Entity Data Model Wizard appears.)
  5. In the Choose Model Contents screen, select Generate from database and then click Next.
  6. In the Choose Your Data Connection screen, provide the connection to your AdventureWorksLT2008 database.
  7. Make sure that the name is AdventureWorksLT2008Entities and that the Save entity connection settings in App.Config as check box is selected, and then click Next.
  8. In the Choose Your Database Objects screen, expand the Tables node, and select the Product and ProductCategory tables.
  9. Click Finish.

To retrieve and present the data:

  1. Open the MainWindow.xaml file.
  2. Set the Width property on the Window to 450.
  3. In the XAML editor, add the following DataGrid tag between the <Grid> and </Grid> tags to add a DataGrid named dataGrid1.
  4. Select the Window.
  5. Using the Properties window or XAML editor, create an event handler for the Window named Window_Loaded for the Loaded event.
  6. Open the code-behind file (MainWindow.xaml.vb or MainWindow.xaml.cs) for the Window.
  7. Add the following code to retrieve only specific values from the joined tables and set the ItemsSource property of the DataGrid to the results of the query:

    using System;
    using System.Collections.Generic;
    using System.Data.Objects;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    
    namespace DataGridSQLExample
    {
        /// <summary> 
        /// Interaction logic for MainWindow.xaml 
        /// </summary> 
        public partial class MainWindow : Window
        {
            AdventureWorksLT2008Entities dataEntities = new AdventureWorksLT2008Entities();
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                ObjectQuery<Product> products = dataEntities.Products;
    
                var query =
                from product in products
                where product.Color == "Red"
                orderby product.ListPrice
                select new { product.Name, product.Color, CategoryName = product.ProductCategory.Name, product.ListPrice };
    
                dataGrid1.ItemsSource = query.ToList();
            }
        }
    }
    
  8. Run the example. You should see a DataGrid that displays data.

When I run I get this error:

Cannot implicitly convert type 'System.Data.Entity.DbSet' to 'System.Data.Objects.ObjectQuery' on this line: ObjectQuery products = dataEntities.Products;

Nothing I have tried works - any suggestions?

like image 285
user1643977 Avatar asked Sep 03 '12 18:09

user1643977


2 Answers

You can use directly , you don't need to cast

    var query =
    from product in dataEntities.Products
    where product.Color == "Red"
    orderby product.ListPrice
    select new { product.Name, product.Color, CategoryName = product.ProductCategory.Name, product.ListPrice };

Nota :

ObjectQuery represents a typical query that is executed on a conceptual model in the context of a given object.

dataEntities.Products represents set of datas

like image 58
Aghilas Yakoub Avatar answered Sep 20 '22 05:09

Aghilas Yakoub


I realize this is really old but you could also do the following:

Change:

using System.Data.Objects;

To:

using System.Data.Entity;

and Change:

ObjectQuery&lt;Product&gt; products = dataEntities.Products;

To:

DbSet&lt;Product&gt; products = dataEntities.Products;
like image 31
Keith Hollen Avatar answered Sep 20 '22 05:09

Keith Hollen