LINQ表間關(guān)系查詢
XX有很多值得學(xué)習(xí)的地方,這里我們主要介紹LINQ表間關(guān)系查詢,包括介紹EntitySet和EntytyRef等方面。
LINQ表間關(guān)系查詢
EnitySet類型為一對多關(guān)系中的“多”方的結(jié)果提供集合。與[Association]屬性結(jié)合使用來定義并表示一個關(guān)系。OtherKey特性,指定在關(guān)聯(lián)的另一端上作為鍵值的、目標(biāo)實(shí)體類的一個或多個成員。
EnitityRef與EntitySet相反,用于一對多關(guān)系中的“一”方。與[Association]屬性結(jié)合使用來定義并表示一個關(guān)系。ThisKey表示關(guān)聯(lián)的此端上的鍵值的此實(shí)體類成員。
LINQ表間關(guān)系查詢-EntitySet
- //Student實(shí)體類
 - [Table(Name = "Student")]
 - public class Student
 - {
 - [Column(IsPrimaryKey = true, DbType = "int")]
 - public int ID;
 - [Column(DbType = "varchar(50)")]
 - public string StuName;
 - [Column(DbType = "bit")]
 - public bool Sex;
 - [Column(DbType = "int")]
 - public int Age;
 - private EntitySet _scores;
 - [Association(Storage = "_scores", OtherKey = "StudentID")]
 - public EntitySet Score
 - {
 - get { return this._scores; }
 - set { this._scores.Assign(value); }
 - }
 - }
 - //Scores實(shí)體類
 - [Table(Name = "Score")]
 - public class Score
 - {
 - [Column(IsPrimaryKey = true, DbType = "int")]
 - public int ID;
 - [Column(DbType = "int")]
 - public int StudentID;
 - [Column(DbType = "float")]
 - public float Math;
 - [Column(DbType = "float")]public float Chinese;
 - [Column(DbType = "float")]
 - public float English;
 - [Column(DbType = "Datetime")]
 - public DateTime Times;
 - }
 - public class TestDB : DataContext
 - {
 - public TestDB(string constr)
 - : base(constr)
 - { }
 - public Table Student;
 - public Table Scores;
 - }
 - static string constr = "server=.;database=test;uid=sa;pwd=sa;";
 - static void Main()
 - {
 - //調(diào)用存儲課程
 - TestDB Test = new TestDB(constr);
 - IQueryable s = from stu in Test.Student
 - select stu;
 - foreach (var v in s)
 - {
 - Console.WriteLine(v.StuName);
 - foreach (var o in v.Score)
 - {
 - Console.WriteLine(" 編號:{0},學(xué)生姓名:{1},學(xué)生年齡:{2},
 
語文成績:{3},考試時間:{4}", v.ID, v.StuName, v.Age,
o.Chinese, o.Times.ToString("yyyy年MM月dd日"));- }
 - }
 - }
 
表間關(guān)系查詢-EntytyRef
- //Student實(shí)體類
 - [Table(Name = "Student")]
 - public class Student
 - {
 - [Column(IsPrimaryKey = true, DbType = "int")]
 - public int ID;
 - [Column(DbType = "varchar(50)")]
 - public string StuName;
 - [Column(DbType = "bit")]
 - public bool Sex;
 - [Column(DbType = "int")]
 - public int Age;
 - }
 - //Scores實(shí)體類
 - [Table(Name = "Score")]
 - public class Score
 - {
 - [Column(IsPrimaryKey = true, DbType = "int")]
 - public int ID
 - [Column(DbType = "int")]
 - public int StudentID;
 - [Column(DbType = "float")]
 - public float Math;
 - [Column(DbType = "float")]
 - public float Chinese;
 - [Column(DbType = "float")]
 - public float English;
 - [Column(DbType = "Datetime")]
 - public DateTime Times;
 - private EntityRef _Student;
 - [Association(Storage = "_Student", ThisKey = "StudentID")]
 - public Student Student
 - {
 - get { return this._Student.Entity; }
 - set { this._Student.Entity = value; }
 - }
 - }
 - public class TestDB : DataContext
 - {
 - public TestDB(string constr)
 - : base(constr)
 - { }
 - public Table Student;
 - public Table Scores;
 - }
 - static string constr = "server=.;database=test;uid=sa;pwd=sa;";
 - static void Main()
 - {
 - //調(diào)用存儲課程
 - TestDB Test = new TestDB(constr);
 - var query = from sco in Test.Scores
 - select sco;
 - foreach (var s in query)
 - {
 - Console.WriteLine(" 編號:{0},學(xué)生姓名:{1},學(xué)生年齡:{2},
 
語文成績:{3},考試時間:{4}", s.StudentID ,s.Student.StuName,
s.Student.Age,s.Chinese, s.Times.ToString("yyyy年MM月dd日"));- }
 - }
 
【編輯推薦】















 
 
 
 
 
 
 