LINQ表间关系查询
创始人
2024-06-07 02:11:24
0

XX有很多值得学习的地方,这里我们主要介绍LINQ表间关系查询,包括介绍EntitySet和EntytyRef等方面。

LINQ表间关系查询

EnitySet类型为一对多关系中的“多”方的结果提供集合。与[Association]属性结合使用来定义并表示一个关系。OtherKey特性,指定在关联的另一端上作为键值的、目标实体类的一个或多个成员。

EnitityRef与EntitySet相反,用于一对多关系中的“一”方。与[Association]属性结合使用来定义并表示一个关系。ThisKey表示关联的此端上的键值的此实体类成员。

LINQ表间关系查询-EntitySet

  1. //Student实体类  
  2. [Table(Name = "Student")]  
  3. public class Student  
  4. {  
  5. [Column(IsPrimaryKey = trueDbType = "int")]  
  6. public int ID;  
  7. [Column(DbType = "varchar(50)")]  
  8. public string StuName;  
  9. [Column(DbType = "bit")]  
  10. public bool Sex;  
  11. [Column(DbType = "int")]  
  12. public int Age;  
  13. private EntitySet _scores;  
  14. [Association(Storage = "_scores"OtherKey = "StudentID")]  
  15. public EntitySet Score  
  16. {  
  17. get { return this._scores; }  
  18. set { this._scores.Assign(value); }  
  19. }  
  20. }  
  21. //Scores实体类  
  22. [Table(Name = "Score")]  
  23. public class Score  
  24. {  
  25. [Column(IsPrimaryKey = trueDbType = "int")]  
  26. public int ID;  
  27. [Column(DbType = "int")]  
  28. public int StudentID;  
  29. [Column(DbType = "float")]  
  30. public float Math;  
  31. [Column(DbType = "float")]public float Chinese;  
  32. [Column(DbType = "float")]  
  33. public float English;  
  34. [Column(DbType = "Datetime")]  
  35. public DateTime Times;  
  36. }  
  37. public class TestDB : DataContext  
  38. {  
  39. public TestDB(string constr)  
  40. : base(constr)  
  41. { }  
  42. public Table Student;  
  43. public Table Scores;  
  44. }  
  45. static string constr = "server=.;database=test;uid=sa;pwd=sa;";  
  46. static void Main()  
  47. {  
  48. //调用存储课程  
  49. TestDB Test = new TestDB(constr);  
  50. IQueryable s = from stu in Test.Student  
  51. select stu;  
  52. foreach (var v in s)  
  53. {  
  54. Console.WriteLine(v.StuName);  
  55. foreach (var o in v.Score)  
  56. {  
  57. Console.WriteLine(" 编号:{0},学生姓名:{1},学生年龄:{2},
    语文成绩:{3},考试时间:{4}", v.ID, v.StuName, v.Age, 
    o.Chinese, o.Times.ToString("yyyy年MM月dd日"));  
  58. }  
  59. }  

表间关系查询-EntytyRef

  1. //Student实体类  
  2. [Table(Name = "Student")]  
  3. public class Student  
  4. {  
  5. [Column(IsPrimaryKey = trueDbType = "int")]  
  6. public int ID;  
  7. [Column(DbType = "varchar(50)")]  
  8. public string StuName;  
  9. [Column(DbType = "bit")]  
  10. public bool Sex;  
  11. [Column(DbType = "int")]  
  12. public int Age;  
  13. }  
  14. //Scores实体类  
  15. [Table(Name = "Score")]  
  16. public class Score  
  17. {  
  18. [Column(IsPrimaryKey = trueDbType = "int")]  
  19. public int ID  
  20. [Column(DbType = "int")]  
  21. public int StudentID;  
  22. [Column(DbType = "float")]  
  23. public float Math;  
  24. [Column(DbType = "float")]  
  25. public float Chinese;  
  26. [Column(DbType = "float")]  
  27. public float English;  
  28. [Column(DbType = "Datetime")]  
  29. public DateTime Times;  
  30. private EntityRef _Student;  
  31. [Association(Storage = "_Student"ThisKey = "StudentID")]  
  32. public Student Student  
  33. {  
  34. get { return this._Student.Entity; }  
  35. set { this._Student.Entity = value; }  
  36. }  
  37. }  
  38. public class TestDB : DataContext  
  39. {  
  40. public TestDB(string constr)  
  41. : base(constr)  
  42. { }  
  43. public Table Student;  
  44. public Table Scores;  
  45. }  
  46. static string constr = "server=.;database=test;uid=sa;pwd=sa;";  
  47. static void Main()  
  48. {  
  49. //调用存储课程  
  50. TestDB Test = new TestDB(constr);  
  51. var query = from sco in Test.Scores  
  52. select sco;  
  53. foreach (var s in query)  
  54. {  
  55. Console.WriteLine(" 编号:{0},学生姓名:{1},学生年龄:{2},
    语文成绩:{3},考试时间:{4}", s.StudentID ,s.Student.StuName, 
    s.Student.Age,s.Chinese, s.Times.ToString("yyyy年MM月dd日"));  
  56. }  

【编辑推荐】

  1. LINQ to SQL查询分析
  2. LINQ查询架构简单介绍
  3. LINQ to SQL映射关系概述
  4. LINQ To SQL对象模型浅析
  5. LINQ to SQL映射列描述

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...