0

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Hank
  • 189
  • 2
  • 7
  • 20
  • Well is there any reason you think you need an `ObjectQuery`? I'd probably just declare the variable as being of type `IQueryable`... – Jon Skeet Jun 23 '16 at 22:29
  • Possible duplicate of http://stackoverflow.com/a/11262713/5922757 – Jezor Jun 23 '16 at 22:41
  • Nothing particular, only try to follow the examples. I will give IQueryable a try. – Hank Jun 24 '16 at 17:26

1 Answers1

1

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
                             ...
                        };
Julian A
  • 334
  • 2
  • 9