Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the method of querying the relationship between LINQ tables

Shulou Source: shulou.com Published: 2022-06-02 08:59:57 10月04日 Update

This article mainly explains "what is the method of querying the relationship between LINQ tables". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the method of querying the relationship between LINQ tables?"

Query the relationship between LINQ tables

The EnitySet type provides a collection of results for the "many" parties in an one-to-many relationship. Used in conjunction with the [Association] attribute to define and represent a relationship. The OtherKey property that specifies one or more members of the target entity class that act as keys on the other end of the association.

EnitityRef, in contrast to EntitySet, is used as a "one" party in an one-to-many relationship. Used in conjunction with the [Association] attribute to define and represent a relationship. ThisKey represents this entity class member of the key value on this end of the association.

Query the relationship between LINQ tables-EntitySet

/ / Student entity class

[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 entity class

[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 ()

{

/ / call the storage course

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 ("serial number: {0}, student name: {1}, student age: {2})

Chinese scores: {3}, examination time: {4} ", v.ID, v.StuName, v.Age

O.Chinese, o.Times.ToString ("yyy year MM month dd day")

}

}

}

Inter-table relationship query-EntytyRef

/ / Student entity class

[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 entity class

[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 ()

{

/ / call the storage course

TestDB Test = new TestDB (constr)

Var query = from sco in Test.Scores

Select sco

Foreach (var s in query)

{

Console.WriteLine ("serial number: {0}, student name: {1}, student age: {2})

Chinese scores: {3}, examination time: {4} ", s.StudentID, s.Student.StuName

S.Student.AgeJournal s.Chinese, s.Times.ToString ("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

}

}

At this point, I believe you have a deeper understanding of "what is the method of querying the relationship between LINQ tables". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Query entity method student content name attribute age member score time examination time language course association storage learning examination practical deeper Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL macOS MariaDB NVidia OPPO Reno