无法将System.Data.Entity.DbSet <DataGridSQLExample.Product>类型隐式转换为System.Data.Objects.ObjectQuery <DataGridSQLExample.Product>

use*_*977 7 sql database visual-studio-2012

我使用的是Visual Studio 2012 C Sharp(C#)和SQL Server Express.我正在尝试通过MSDN演练:在DataGrid控件中显示来自SQL Server数据库的数据.我添加了推荐的AdventureWorksLT2008示例数据库.以下是我遵循的说明:

  1. 在Visual Basic或C#中创建一个新的WPF应用程序项目,并将其命名为DataGridSQLExample.
  2. 在解决方案资源管理器中,右键单击项目,指向"添加",然后选择"新建项".(出现"添加新项"对话框.)
  3. 在Installed Templates窗格中,选择Data,然后在模板列表中选择ADO.NET Entity Data Model.
  4. 命名该文件AdventureWorksModel.edmx,然后单击"添加".(出现实体数据模型向导.)
  5. 在Choose Model Contents屏幕中,选择Generate from database,然后单击Next.
  6. 在"选择数据连接"屏幕中,提供与AdventureWorksLT2008数据库的连接.
  7. 确保名称为AdventureWorksLT2008Entities,并App.Config选中" 在实例中保存实体连接设置"复选框,然后单击"下一步".
  8. 在Choose Your Database Objects屏幕中,展开Tables节点,然后选择Product和ProductCategory表.
  9. 单击完成.

要检索和显示数据:

  1. 打开MainWindow.xaml文件.
  2. 将窗口上的Width属性设置为450.
  3. 在XAML编辑器中,在<Grid>和标记之间添加以下DataGrid标记</Grid>以添加名为的DataGrid dataGrid1.
  4. 选择窗口.
  5. 使用"属性"窗口或XAML编辑器,为Window_Loaded为Loaded事件命名的Window创建事件处理程序.
  6. 打开Window 的代码隐藏文件(MainWindow.xaml.vbMainWindow.xaml.cs).
  7. 添加以下代码以仅从联接表中检索特定值ItemsSource,并将DataGrid 的属性设置为查询结果:

    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();
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  8. 运行示例.您应该看到一个显示数据的DataGrid.

当我运行时,我收到此错误:

无法在此行上隐式转换类型'System.Data.Entity.DbSet'为'System.Data.Objects.ObjectQuery':ObjectQuery products = dataEntities.Products;

我没有尝试任何工作 - 任何建议?

Agh*_*oub 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 };
Run Code Online (Sandbox Code Playgroud)

Nota:

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

dataEntities.Products表示数据集

  • 是的,这当然有效.我很困惑为什么微软在他们的演练中使用了违规行(尽管,到现在为止,我不应该这样.谢谢. (2认同)