7

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?

4

2 回答 2

7

可以直接使用,不需要强制转换

    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 };

注意:

ObjectQuery 表示在给定对象的上下文中在概念模型上执行的典型查询。

dataEntities.Products 表示数据集

于 2012-09-03T18:12:21.257 回答
1

我意识到这真的很旧,但您也可以执行以下操作:

改变:

using System.Data.Objects;

至:

using System.Data.Entity;

和改变:

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

至:

DbSet&lt;Product&gt; products = dataEntities.Products;
于 2020-02-12T01:46:07.853 回答