溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

LINQ表間關(guān)系查詢的方法是什么

發(fā)布時間:2021-12-02 09:23:38 來源:億速云 閱讀:91 作者:iii 欄目:編程語言

本篇內(nèi)容主要講解“LINQ表間關(guān)系查詢的方法是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“LINQ表間關(guān)系查詢的方法是什么”吧!

LINQ表間關(guān)系查詢

EnitySet類型為一對多關(guān)系中的“多”方的結(jié)果提供集合。與[Association]屬性結(jié)合使用來定義并表示一個關(guān)系。OtherKey特性,指定在關(guān)聯(lián)的另一端上作為鍵值的、目標(biāo)實體類的一個或多個成員。

EnitityRef與EntitySet相反,用于一對多關(guān)系中的“一”方。與[Association]屬性結(jié)合使用來定義并表示一個關(guān)系。ThisKey表示關(guān)聯(lián)的此端上的鍵值的此實體類成員。

LINQ表間關(guān)系查詢-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. //調(diào)用存儲課程  

  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},學(xué)生姓名:{1},學(xué)生年齡:{2},
    語文成績:{3},考試時間:{4}", v.ID, v.StuName, v.Age, 
    o.Chinese, o.Times.ToString("yyyy年MM月dd日"));  

  58. }  

  59. }  

表間關(guān)系查詢-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. //調(diào)用存儲課程  

  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},學(xué)生姓名:{1},學(xué)生年齡:{2},
    語文成績:{3},考試時間:{4}", s.StudentID ,s.Student.StuName, 
    s.Student.Age,s.Chinese, s.Times.ToString("yyyy年MM月dd日"));  

  56. }  

到此,相信大家對“LINQ表間關(guān)系查詢的方法是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI