Csharp/CSharp Tutorial/LINQ/SQL

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Use LINQ to SQL

using System;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;
static class HelloLinqToSql {
    [Table(Name = "Contacts")]
    class Contact {
        [Column(IsPrimaryKey = true)]
        public int ContactID { get; set; }
        [Column(Name = "ContactName")]
        public string Name { get; set; }
        [Column]
        public string City { get; set; }
    }
    static void Main() {
        string path = System.IO.Path.GetFullPath("northwnd.mdf");
        DataContext db = new DataContext(path);
        var contacts =
          from contact in db.GetTable<Contact>()
          where contact.City == "Paris"
          select contact;
        foreach (var contact in contacts)
            Console.WriteLine("Bonjour " + contact.Name);
    }
}