本篇内容主要讲解“LINQ表间关系查询的方法是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“LINQ表间关系查询的方法是什么”吧!
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表间关系查询的方法是什么”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。