简体   繁体   中英

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#. I tried to follow this examples on msdn , but have encountered a type conversion error. I am using Visual studio 2013.

I am connected to a SQL server, and created an ado.net data model named myEntity. The model contains several tables, one of them, Theater, is what I try to display on the screen.

Here is what I have: On the MainWindow.xaml file I have

<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:

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>'

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).

So you simply have to change the type of the variable theaters from ObjectQuery<Theater> to 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
                             ...
                        };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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