Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of querying the relationship between LINQ tables

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report