简体   繁体   English

C#中的数据类型转换错误

[英]data type conversion error in C#

I'd like to display data that are stored in a SQL server database in a data grid using C#. 我想使用C#在数据网格中显示存储在SQL Server数据库中的数据。 I tried to follow this examples on msdn , but have encountered a type conversion error. 我尝试在msdn上遵循此示例 ,但是遇到类型转换错误。 I am using Visual studio 2013. 我正在使用Visual Studio 2013。

I am connected to a SQL server, and created an ado.net data model named myEntity. 我已连接到SQL Server,并创建了一个名为myEntity的ado.net数据模型。 The model contains several tables, one of them, Theater, is what I try to display on the screen. 该模型包含多个表,其中一个表是Theater,这是我尝试在屏幕上显示的表。

Here is what I have: On the MainWindow.xaml file I have 这是我所拥有的:在MainWindow.xaml文件中,我拥有

<Page x:Class="XYZ.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="theater List" Height="350" Width="525"
        Loaded="Data_Loaded">
    <Grid>
        <DataGrid Name="dataGrid1"></DataGrid>
    </Grid>
</Page>

On the MainWindow.xaml.cs file I have: 在MainWindow.xaml.cs文件中,我具有:

using System.Data.Entity.Core.Objects;
using System.Windows;
using System.Windows.Controls;
using System.Linq;

namespace XYZ
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public sealed partial class MainPage : Page
    {
        myEntities dataEntites = new myEntities();

        public MainPage()
        {
            InitializeComponent();
        }

        private void Data_Loaded(object sender, RoutedEventArgs e)
        {
                ObjectQuery<Theater> theaters = dataEntites.Theaters;

                var query = from theater in theaters
                            where theater.type == "Big"
                            orderby theater.id
                            select new
                            {
                                theater.State,
                                theater.City,
                                theater.Type, 
                                theater.Id, 
                                theater.Name,
                                theater.Capacity
                                 ...
                            };

                dataGrid1.ItemsSource = query.ToList();  
        }
    }
}

I encountered an error message on the line 我在网上遇到错误消息

ObjectQuery<Theater> theaters = dataEntites.Theaters;

Which states: 哪个状态:

Cannot implicitly convert type 'System.Data.Entity.DbSet<XYZ.Theater>' to 'System.Data.Entity.Core.Objects.ObjectQuery<XYZ.Theater>' 无法将类型'System.Data.Entity.DbSet<XYZ.Theater>'隐式转换为'System.Data.Entity.Core.Objects.ObjectQuery<XYZ.Theater>'

How could I fix this? 我该如何解决? Thanks. 谢谢。

The problem here is that System.Data.Entity.Core.Objects.ObjectQuery<T> does not inherit from System.Data.Entity.DbSet<T> and therefore an object of one class cannot be converted to another implicitly (expect the implicit type conversion operator would be overridden which is not the case). 这里的问题是System.Data.Entity.Core.Objects.ObjectQuery<T>不能从System.Data.Entity.DbSet<T>继承,因此一个类的对象不能隐式转换为另一个(请注意隐式类型转换运算符将被覆盖(不是这种情况)。

So you simply have to change the type of the variable theaters from ObjectQuery<Theater> to DbSet<Theater> : 因此,您只需要将变量剧院的类型从ObjectQuery<Theater>更改为DbSet<Theater>

                DbSet<Theater> theaters = dataEntites.Theaters;

                var query = from theater in theaters 
                        where theater.type == "Big"
                        orderby theater.id
                        select new
                        {
                            theater.State,
                            theater.City,
                            theater.Type, 
                            theater.Id, 
                            theater.Name,
                            theater.Capacity
                             ...
                        };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM