LINQ实战一
Code
1
2
3namespace LinqConsoleApp
4{
5 [Table(Name = "Customers")]
6 public class Customer
7 {
8 private string _CustomerID;
9
10 [Column(IsPrimaryKey=true, Storage="_CustomerID")]
11 public string CustomerID
12 {
13 get
14 {
15 return this._CustomerID;
16 }
17 set
18 {
19 this._CustomerID = value;
20 }
21
22 }
23
24 private string _City;
25 [Column(Storage="_City")]
26 public string City
27 {
28 get
29 {
30 return this._City;
31 }
32 set
33 {
34 this._City=value;
35 }
36 }
37
38
39
40
41
42
43
44 }
45
46
47
48 class Program
49 {
50 static void Main(string[] args)
51 {
52 // Use a connection string.
53 DataContext db = new DataContext("server=.;database=Northwind;uid=sa;pwd=local;");
54
55 // Get a typed table to run queries.
56 Table<Customer> Customers = db.GetTable<Customer>();
57 // Attach the log to show generated SQL.
58 db.Log = Console.Out;
59
60 // Query for customers in London.
61 IQueryable<Customer> custQuery = from cust in Customers where cust.City == "London" select cust;
62
63
64 foreach (Customer cust in custQuery)
65 {
66 Console.WriteLine("ID={0}, City={1}", cust.CustomerID,cust.City);
67 }
68
69 // Prevent console window from closing.
70 Console.ReadLine();
71
72 }
73 }
74}
75
1
2
3namespace LinqConsoleApp
4{
5 [Table(Name = "Customers")]
6 public class Customer
7 {
8 private string _CustomerID;
9
10 [Column(IsPrimaryKey=true, Storage="_CustomerID")]
11 public string CustomerID
12 {
13 get
14 {
15 return this._CustomerID;
16 }
17 set
18 {
19 this._CustomerID = value;
20 }
21
22 }
23
24 private string _City;
25 [Column(Storage="_City")]
26 public string City
27 {
28 get
29 {
30 return this._City;
31 }
32 set
33 {
34 this._City=value;
35 }
36 }
37
38
39
40
41
42
43
44 }
45
46
47
48 class Program
49 {
50 static void Main(string[] args)
51 {
52 // Use a connection string.
53 DataContext db = new DataContext("server=.;database=Northwind;uid=sa;pwd=local;");
54
55 // Get a typed table to run queries.
56 Table<Customer> Customers = db.GetTable<Customer>();
57 // Attach the log to show generated SQL.
58 db.Log = Console.Out;
59
60 // Query for customers in London.
61 IQueryable<Customer> custQuery = from cust in Customers where cust.City == "London" select cust;
62
63
64 foreach (Customer cust in custQuery)
65 {
66 Console.WriteLine("ID={0}, City={1}", cust.CustomerID,cust.City);
67 }
68
69 // Prevent console window from closing.
70 Console.ReadLine();
71
72 }
73 }
74}
75