Is this an example of LINQ-to-SQL?

Posted by Edward Tanguay on Stack Overflow See other posts from Stack Overflow or by Edward Tanguay
Published on 2009-02-25T13:20:56Z Indexed on 2010/04/27 3:33 UTC
Read the original article Hit count: 333

Filed under:
|
|

I made a little WPF application with a SQL CE database.

I built the following code with LINQ to get data out of the database, which was surprisingly easy. So I thought "this must be LINQ-to-SQL".

Then I did "add item" and added a "LINQ-to-SQL classes" .dbml file, dragged my table onto the Object Relational Designer but it said, "The selected object uses an unsupported data provider."

So then I questioned whether or not the following code actually is LINQ-to-SQL, since it indeed allows me to access data from my SQL CE database file, yet officially "LINQ-to-SQL" seems to be unsupported for SQL CE.

So is the following "LINQ-to-SQL" or not?

using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Windows;

namespace TestLinq22
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            MainDB db = new MainDB(@"Data Source=App_Data\Main.sdf");
            var customers = from c in db.Customers
                            select new {c.FirstName, c.LastName};
            TheListBox.ItemsSource = customers;
        }
    }

    [Database(Name = "MainDB")]
    public class MainDB : DataContext
    {
        public MainDB(string connection) : base(connection) { }
        public Table<Customers> Customers;
    }

    [Table(Name = "Customers")]
    public class Customers
    {
        [Column(DbType = "varchar(100)")]
        public string FirstName;

        [Column(DbType = "varchar(100)")]
        public string LastName;
    }

}

© Stack Overflow or respective owner

Related posts about linq-to-sql

Related posts about sql-server-ce