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日"));- }
- }
【编辑推荐】