using System; using System.Linq; using System.Data.Linq; // 這個命名空間在單獨的組件 System.Data.Linq.dll 中
public class DLinqTest { public static void DoSomeThing() { // 鏈接字符串 string connectionString = "Data Source=192.168.5.2;Initial Catalog=pubs;Persist Security Info=True;User ID=sa;PassWord=******";
// 我們就是通過使用 DataContext 來 DLINQ鏈接數據庫的。 DataContext db = new DataContext(connectionString); Table<Authors> authors = db.GetTable<Authors>(); var users = from a in authors orderby a.au_id select a; foreach (var a in users) { Console.WriteLine(a.au_id); } } }
// 數據庫中的表結構影射的實體對象,注意其中的 Attribute.
[Table(Name = "authors")] public class Authors { [Column(IsPRimaryKey = true)] public string au_id { get; set; } [Column] public string au_lname { get; set; } [Column] public string au_fname { get; set; } [Column] public string phone { get; set; } [Column] public string city { get; set; } [Column] public string state { get; set; } [Column] public string zip { get; set; } [Column] public bool contract { get; set; } }
using System; using System.Linq; using System.Xml.Linq;
public class XLINQ { public static void DoSomeThing() { XElement feed = XElement.Load("http://blog.joycode.com/ghj/Rss.aspx"); if (feed.Element("channel") == null) return;
var rss = from item in feed.Element("channel").Elements("item") select new { title = item.Element("title").Value, link = item.Element("link").Value }; foreach (var item in rss) { Console.WriteLine(item.link); Console.WriteLine(item.title); Console.WriteLine("*****"); } } }