您好,登錄后才能下訂單哦!
本篇內容主要講解“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表間關系查詢的方法是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。