Lambda 转换 SQL ORM 工具 SqlRepoEx

主要解决:

1、解决拼接语句,使用where以外方法时,缺少Where子句时的错误;
2、解决多条件拼接Where;
3、增加操作时,不再受限于实例必需有Id的自增自段

Example

IRepository<ToDo> repository = repositoryFactory.Create<ToDo>();
            var results = repository.Query()
                         .Select(e => e.Id, e => e.Task, e => e.CreatedDate);
 results = results.Where(e => e.IsCompleted == false);
 results = results.Where(e => e.Id == 3);

未改之前

SELECT [dbo].[ToDo].[Id]
, [dbo].[ToDo].[Task]
, [dbo].[ToDo].[CreatedDate]
FROM [dbo].[ToDo]
WHERE ([dbo].[ToDo].[IsCompleted] = 0)
WHERE ([dbo].[ToDo].[Id] = 3);

更改后

SELECT [dbo].[ToDo].[Id]
, [dbo].[ToDo].[Task]
, [dbo].[ToDo].[CreatedDate]
FROM [dbo].[ToDo]
WHERE ([dbo].[ToDo].[IsCompleted] = 0)
And ([dbo].[ToDo].[Id] = 3);

指定非自增字段

var repository = this.repositoryFactory.Create<DoitTest>();
DoitTest doitTest = new DoitTest();

doitTest.TestRmk = "测试";
doitTest.TestBool = true;
doitTest.TestId = 123;
Console.WriteLine(repository.Insert().UsingIdField(d => d.TestId, false).For(doitTest).Sql());

生成的SQL

INSERT [dbo].[DoitTest]([TestId], [TestRmk], [TestBool])
VALUES(123, '测试', 1);

指定非自增字段

var repository = this.repositoryFactory.Create<DoitTest>();
DoitTest doitTest = new DoitTest();

doitTest.TestRmk = "测试";
doitTest.TestBool = true;
doitTest.TestId = 123;
Console.WriteLine(repository.Insert().UsingIdField(d => d.TestId).For(doitTest).Sql());

生成的SQL

INSERT [dbo].[DoitTest]([TestRmk], [TestBool])
VALUES('测试', 1);
SELECT *
FROM [dbo].[DoitTest]
WHERE [TestId] = SCOPE_IDENTITY();

原项目中的例子:

public class GettingStarted
{
    private IRepositoryFactory repositoryFactory;

    public GettingStarted(IRepositoryFactory repositoryFactory)
    {
        this.repositoryFactory = repositoryFactory;
    }

    public void DoIt()
    {
         var repository = this.repositoryFactory.Create<ToDo>();
         var results = repository.Query()
         .Select(e => e.Id, e => e.Task, e => e.CreatedDate)
         .Where(e => e.IsCompleted == false)
         .Go();
    }
}

Generates the following SQL statement and maps the results back to the list of ToDo objects.

SELECT [dbo].[ToDo].[Id], [dbo].[ToDo].[Task], [dbo].[ToDo].[CreatedDate]
FROM [dbo].[ToDo]
WHERE [dbo].[ToDo].[IsCompleted] = 0;

2018-9-25增加分页操作

var repository = RepoFactory.Create<ToDo>();
            var results = repository.Query()
                                    .Select(e => e.Id, e => e.Task, e => e.CreatedDate)
                                    .OrderBy(e => e.Id)
                                    .Page(10, 3)
                                    .Go();

2018-9-25增加存储OUTPUT参数返回

本文来自云栖社区合作伙伴“开源中国”

本文作者:h4cd

原文链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值