虽然没人看不过还是继续写
上一篇写的是直接传SQL语句的方法 在C#代码中看到SQL怎么都觉得不舒服好了现在看下C#的写法
var z = new SelectSqlSection<RJ.DModel.Users>();
z.AddColumn(a => new { s = (a.D_Id * 5), b = a.U_UserName });
db.ToIList<RJ.Model.Users>(z)
生成的SQL语句如下
SELECT ([Users].[D_Id] * @pj02gde4ln6dfwj) [s],[Users].[U_UserName] [b] FROM [Users]
看起是不是要舒服点了呢如果不想用匿名对象的也可以写成数组
var z = new SelectSqlSection<RJ.DModel.Users>();
z.AddColumn(a => new[] { (a.D_Id * 5).Alias("D_Id"), a.U_UserName });
db.ToIList<RJ.Model.Users>(z)
生成的SQL语句如下
SELECT ([Users].[D_Id] * @pq6xgwpsclvtuco) [D_Id],[Users].[U_UserName] FROM [Users]
只有一个字段时也可以写成
var z = new SelectSqlSection<RJ.DModel.Users>();
z.AddColumn(a => a.D_Id);
db.ToIList<RJ.Model.Users>(z)
生成的SQL语句如下
SELECT [Users].[D_Id] FROM [Users]
如果只想取单条数据时可以写成
RJ.Model.Users user=db.Single<RJ.Model.Users>(z);
生成的SQL语句如下
SELECT TOP 1 [Users].[D_Id] FROM [Users]
不想返回对象也可以直接返回
DataSetDataSet user=db.ToDataSet(z);
在来看看筛选
var z = new SelectSqlSection<RJ.DModel.Users>();
z.Where(a => a.D_Id > 5);
SELECT * FROM [Users] WHERE [Users].[D_Id] > @pjackrftwkjpya0
多个条件
var z = new SelectSqlSection<RJ.DModel.Users>();
z.Where(a => (a.D_Id > 5 | a.D_Id == 2) & a.U_Id > 5);
SELECT * FROM [Users] WHERE (([Users].[D_Id] > @pn13yc95bj366hk OR [Users].[D_Id] = @p25vpkscoijdtlx) AND [Users].[U_Id] > @pas9pm39urm0s8l)
联表查询
Join
var z = new SelectSqlSection<RJ.DModel.Users>();
z.Join<RJ.DModel.Department>((a,b)=>a.D_Id==b.D_Id);
SELECT * FROM [Users] INNER JOIN [Department] ON [Users].[D_Id] = [Department].[D_Id]
LeftJoin
var z = new SelectSqlSection<RJ.DModel.Users>();
z.LeftJoin<RJ.DModel.Department>((a,b)=>a.D_Id==b.D_Id);
RightJoin
var z = new SelectSqlSection<RJ.DModel.Users>();
z.RightJoin<RJ.DModel.Department>((a,b)=>a.D_Id==b.D_Id);
SELECT * FROM [Users] LEFT OUTER JOIN [Department] ON [Users].[D_Id] = [Department].[D_Id]
普通的
var z = new SelectSqlSection<RJ.DModel.Users>();
z.AddForm<RJ.DModel.Department>((a,b)=>a.D_Id==b.D_Id);
SELECT * FROM [Users] , [Department] WHERE [Users].[D_Id] = [Department].[D_Id]