001
Code
1string[] strs = { "aaa", "bbbbb", "ccc", "ddddd", "eee", "fffff", "ggg" };
2
3//查询表达式方式
4//IEnumerable<string> result =
5// from str in strs
6// where str.Length == 5
7// orderby str
8// select str.ToUpper();
9
10//Lambda表达式方式
11var result = strs
12 .Where(str => str.Length == 5)
13 .OrderBy(str => str)
14 .Select(str => str.ToUpper());
15
16foreach (string str in result){
17 Console.WriteLine(str);
18}
19
1string[] strs = { "aaa", "bbbbb", "ccc", "ddddd", "eee", "fffff", "ggg" };
2
3//查询表达式方式
4//IEnumerable<string> result =
5// from str in strs
6// where str.Length == 5
7// orderby str
8// select str.ToUpper();
9
10//Lambda表达式方式
11var result = strs
12 .Where(str => str.Length == 5)
13 .OrderBy(str => str)
14 .Select(str => str.ToUpper());
15
16foreach (string str in result){
17 Console.WriteLine(str);
18}
19
002 LINQtoSQL
Code
1[Table(Name="TestTable001")]
2public class TestLINQtoSQL
3{
4 [Column(IsPrimaryKey=true)]
5 public long ID { get; set; }
6
7 [Column]
8 public string Name { get; set; }
9
10 [Column]
11 public int Age { get; set; }
12}
13
14//手动方式
15//DataContext dc = new DataContext(@"Data Source=KENT\SQLEXPRESS; USER ID=sa; PASSWORD=123456; Initial Catalog=TestDataBase;Persist Security Info=True;");
16//Table<TestLINQtoSQL> table = dc.GetTable<TestLINQtoSQL>();
17
18//自动方式
19TestDBML001DataContext dc = new TestDBML001DataContext(@"Data Source=KENT\SQLEXPRESS; USER ID=sa; PASSWORD=123456; Initial Catalog=TestDataBase;Persist Security Info=True;");
20Table<TestDBMLTable> table = dc.TestDBMLTable;
21
22//var result =
23// from cell in table
24// where cell.ID > 10
25// select new { cell.Name, cell.Age };
26
27var result = table.Where(cell => cell.ID > 10).Select(cell => new { cell.Name, cell.Age });
28
29dataGridView1.DataSource = result;
30
1[Table(Name="TestTable001")]
2public class TestLINQtoSQL
3{
4 [Column(IsPrimaryKey=true)]
5 public long ID { get; set; }
6
7 [Column]
8 public string Name { get; set; }
9
10 [Column]
11 public int Age { get; set; }
12}
13
14//手动方式
15//DataContext dc = new DataContext(@"Data Source=KENT\SQLEXPRESS; USER ID=sa; PASSWORD=123456; Initial Catalog=TestDataBase;Persist Security Info=True;");
16//Table<TestLINQtoSQL> table = dc.GetTable<TestLINQtoSQL>();
17
18//自动方式
19TestDBML001DataContext dc = new TestDBML001DataContext(@"Data Source=KENT\SQLEXPRESS; USER ID=sa; PASSWORD=123456; Initial Catalog=TestDataBase;Persist Security Info=True;");
20Table<TestDBMLTable> table = dc.TestDBMLTable;
21
22//var result =
23// from cell in table
24// where cell.ID > 10
25// select new { cell.Name, cell.Age };
26
27var result = table.Where(cell => cell.ID > 10).Select(cell => new { cell.Name, cell.Age });
28
29dataGridView1.DataSource = result;
30
003 强类型的DataContext
Code
1public class MyDataContext : DataContext
2{
3 /**//// <summary>Table : TestLINQtoSQL</summary>
4 public Table<TestLINQtoSQL> tableTest;
5
6 public MyDataContext(string strConn) : base(strConn) { }
7
8 public static MyDataContext CreateDataContext()
9 {
10 return new MyDataContext(@"Data Source=KENT\SQLEXPRESS; USER ID=sa; PASSWORD=123456; Initial Catalog=TestDataBase;Persist Security Info=True;");
11 }
12}
13
14MyDataContext mydc = MyDataContext.CreateDataContext();
15//var result =
16// from cell in mydc.tableTest
17// where cell.ID > 10
18// select new { cell.Name, cell.Age };
19
20var result = mydc.tableTest.Where(cell => cell.ID > 10).Select(cell => new { cell.Name, cell.Age });
21
22dataGridView1.DataSource = result;
23
1public class MyDataContext : DataContext
2{
3 /**//// <summary>Table : TestLINQtoSQL</summary>
4 public Table<TestLINQtoSQL> tableTest;
5
6 public MyDataContext(string strConn) : base(strConn) { }
7
8 public static MyDataContext CreateDataContext()
9 {
10 return new MyDataContext(@"Data Source=KENT\SQLEXPRESS; USER ID=sa; PASSWORD=123456; Initial Catalog=TestDataBase;Persist Security Info=True;");
11 }
12}
13
14MyDataContext mydc = MyDataContext.CreateDataContext();
15//var result =
16// from cell in mydc.tableTest
17// where cell.ID > 10
18// select new { cell.Name, cell.Age };
19
20var result = mydc.tableTest.Where(cell => cell.ID > 10).Select(cell => new { cell.Name, cell.Age });
21
22dataGridView1.DataSource = result;
23
004 表关系映射
Code
1[Table(Name="Person")]
2public class Person
3{
4 [Column(IsPrimaryKey=true)]
5 public long ID { get; set; }
6
7 [Column]
8 public string Name { get; set; }
9
10 [Column]
11 public int Age { get; set; }
12
13 [Column]
14 public long DepartmentID { get; set; }
15
16 private EntityRef<Department> _department;
17
18 [Association(Storage = "_department", ThisKey = "DepartmentID")]
19 public Department Department
20 {
21 get { return this._department.Entity; }
22 set { this._department.Entity = value; }
23 }
24}
25
26[Table(Name="Departments")]
27public class Department
28{
29 [Column(IsPrimaryKey=true)]
30 public long DepartmentID { get; set; }
31
32 [Column]
33 public string Name { get; set; }
34
35 private EntitySet<Person> _persons;
36
37 [Association(Storage = "_persons", OtherKey = "DepartmentID")]
38 public EntitySet<Person> Persons
39 {
40 get { return this._persons; }
41 set { this._persons.Assign(value); }
42 }
43}
44
45MyDataContext mydc = MyDataContext.CreateDataContext();
46var result =
47 from d in mydc.department
48 from p in d.Persons
49 where d.DepartmentID == 1
50 select new { p.ID, p.Name, DepartmentName = d.Name };
51dataGridView1.DataSource = result;
52
1[Table(Name="Person")]
2public class Person
3{
4 [Column(IsPrimaryKey=true)]
5 public long ID { get; set; }
6
7 [Column]
8 public string Name { get; set; }
9
10 [Column]
11 public int Age { get; set; }
12
13 [Column]
14 public long DepartmentID { get; set; }
15
16 private EntityRef<Department> _department;
17
18 [Association(Storage = "_department", ThisKey = "DepartmentID")]
19 public Department Department
20 {
21 get { return this._department.Entity; }
22 set { this._department.Entity = value; }
23 }
24}
25
26[Table(Name="Departments")]
27public class Department
28{
29 [Column(IsPrimaryKey=true)]
30 public long DepartmentID { get; set; }
31
32 [Column]
33 public string Name { get; set; }
34
35 private EntitySet<Person> _persons;
36
37 [Association(Storage = "_persons", OtherKey = "DepartmentID")]
38 public EntitySet<Person> Persons
39 {
40 get { return this._persons; }
41 set { this._persons.Assign(value); }
42 }
43}
44
45MyDataContext mydc = MyDataContext.CreateDataContext();
46var result =
47 from d in mydc.department
48 from p in d.Persons
49 where d.DepartmentID == 1
50 select new { p.ID, p.Name, DepartmentName = d.Name };
51dataGridView1.DataSource = result;
52