本篇内容主要讲解“LINQ表间关系查询的方法是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“LINQ表间关系查询的方法是什么”吧!
成都创新互联是一家专业提供彭州企业网站建设,专注与成都网站设计、网站制作、外贸营销网站建设、H5页面制作、小程序制作等业务。10年已为彭州众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。
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表间关系查询的方法是什么”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
文章标题:LINQ表间关系查询的方法是什么
标题链接:http://scpingwu.com/article/ijeeoi.html