LINQ表間關系查詢
作者:佚名
這里介紹LINQ表間關系查詢,包括介紹EnitityRef與EntitySet相反,用于一對多關系中的“一”方。與[Association]屬性結合使用來定義并表示一個關系。
XX有很多值得學習的地方,這里我們主要介紹LINQ表間關系查詢,包括介紹EntitySet和EntytyRef等方面。
LINQ表間關系查詢
EnitySet類型為一對多關系中的“多”方的結果提供集合。與[Association]屬性結合使用來定義并表示一個關系。OtherKey特性,指定在關聯的另一端上作為鍵值的、目標實體類的一個或多個成員。
EnitityRef與EntitySet相反,用于一對多關系中的“一”方。與[Association]屬性結合使用來定義并表示一個關系。ThisKey表示關聯的此端上的鍵值的此實體類成員。
LINQ表間關系查詢-EntitySet
- //Student實體類
- [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實體類
- [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()
- {
- //調用存儲課程
- 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},學生姓名:{1},學生年齡:{2},
語文成績:{3},考試時間:{4}", v.ID, v.StuName, v.Age,
o.Chinese, o.Times.ToString("yyyy年MM月dd日"));- }
- }
- }
表間關系查詢-EntytyRef
- //Student實體類
- [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實體類
- [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()
- {
- //調用存儲課程
- TestDB Test = new TestDB(constr);
- var query = from sco in Test.Scores
- select sco;
- foreach (var s in query)
- {
- Console.WriteLine(" 編號:{0},學生姓名:{1},學生年齡:{2},
語文成績:{3},考試時間:{4}", s.StudentID ,s.Student.StuName,
s.Student.Age,s.Chinese, s.Times.ToString("yyyy年MM月dd日"));- }
- }
【編輯推薦】
責任編輯:佚名
來源:
IT168