LinqPad工具:帮你快速学习Linq
参考:
http://www.cnblogs.com/li-peng/p/3441729.html
★:linqPad下载地址:http://www.linqpad.net/;
一.基本操作
1. 第一步:连接数据库;
2. 进行具体的linq操作:
执行查询:
二.实战用法:Left Join和Inner Join
1. 首先建数据库:
代码:
create database MyDataDemo go use MyDataDemo go drop table Student create table Classes ( class_Id int , class_Name varchar(100) ) create table Student ( st_Id int, st_Name varchar(100), class_Id int ) insert into Classes values(1,‘OneC‘) insert into Classes values(2,‘TwoC‘) insert into Classes values(3,‘ThreeC‘) insert into Student values(1,‘xiaoMing‘,1) insert into Student values(2,‘zhangqiang‘,2) insert into Student values(3,‘lihong‘,3) insert into Student values(4,‘wangsi‘,1) insert into Student values(5,‘zhaoWu‘,1) insert into Student values(6,‘WangLiu‘,2) |
2. 在linqPad的query标签里把Language 选择为c# Expression ,把Connection 选择数据MyDataDemo 就是我们上边建好的数据库如图:
3. Left Join:
输入最基本的Linq表达式点执行或者F5
from stu in Students join cla in Classes on stu.Class_Id equals cla.Class_Id into MyJoin from grp in MyJoin.DefaultIfEmpty() select new {ClassId=grp.Class_Id,Class_Name=grp.Class_Name,st_Name =stu.St_Name} |
执行结果:
3. inner join:
from stu in Students join cla in Classes on stu.Class_Id equals cla.Class_Id select new {ClassId=cla.Class_Id,Class_Name=cla.Class_Name,st_Name =stu.St_Name} |
执行结果:
补充:
数据表的连接有: 1、内连接(自然连接): 只有两个表相匹配的行才能在结果集中出现 2、外连接: 包括 (1)左外连接(左边的表不加限制) (2)右外连接(右边的表不加限制) (3)全外连接(左右两表都不加限制) 3、自连接(连接发生在一张基表内)