001
1

string[] strs =
{ "aaa", "bbbbb", "ccc", "ddddd", "eee", "fffff", "ggg" };2

3
//查询表达式方式4
//IEnumerable<string> result =5
// from str in strs6
// where str.Length == 57
// orderby str8
// select str.ToUpper();9

10
//Lambda表达式方式11
var result = strs12
.Where(str => str.Length == 5)13
.OrderBy(str => str)14
.Select(str => str.ToUpper());15

16

foreach (string str in result)
{17
Console.WriteLine(str);18
}19

002 LINQtoSQL
1
[Table(Name="TestTable001")]2
public class TestLINQtoSQL3


{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
//自动方式19
TestDBML001DataContext dc = new TestDBML001DataContext(@"Data Source=KENT\SQLEXPRESS; USER ID=sa; PASSWORD=123456; Initial Catalog=TestDataBase;Persist Security Info=True;");20
Table<TestDBMLTable> table = dc.TestDBMLTable;21

22
//var result =23
// from cell in table24
// where cell.ID > 1025
// select new { cell.Name, cell.Age };26

27

var result = table.Where(cell => cell.ID > 10).Select(cell => new
{ cell.Name, cell.Age });28

29
dataGridView1.DataSource = result;30

003 强类型的DataContext
1
public class MyDataContext : DataContext2


{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

14
MyDataContext mydc = MyDataContext.CreateDataContext();15
//var result =16
// from cell in mydc.tableTest17
// where cell.ID > 1018
// select new { cell.Name, cell.Age };19

20

var result = mydc.tableTest.Where(cell => cell.ID > 10).Select(cell => new
{ cell.Name, cell.Age });21

22
dataGridView1.DataSource = result;23

004 表关系映射
1
[Table(Name="Person")]2
public class Person3


{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 Department20

{21

get
{ return this._department.Entity; }22

set
{ this._department.Entity = value; }23
}24
}25

26
[Table(Name="Departments")]27
public class Department28


{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> Persons39

{40

get
{ return this._persons; }41

set
{ this._persons.Assign(value); }42
}43
}44

45
MyDataContext mydc = MyDataContext.CreateDataContext();46
var result =47
from d in mydc.department48
from p in d.Persons49
where d.DepartmentID == 150

select new
{ p.ID, p.Name, DepartmentName = d.Name };51
dataGridView1.DataSource = result;52

浙公网安备 33010602011771号