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日"));- }
- }
分享名称:LINQ表间关系查询
文章出自:http://www.shufengxianlan.com/qtweb/news1/450201.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联