温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

LINQ表间关系查询的方法是什么

发布时间:2021-12-02 09:23:38 来源:亿速云 阅读:85 作者:iii 栏目:编程语言

本篇内容主要讲解“LINQ表间关系查询的方法是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“LINQ表间关系查询的方法是什么”吧!

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. }  

到此,相信大家对“LINQ表间关系查询的方法是什么”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI