LambdaToSql 發佈 蘭姆達轉換sql

文檔目錄索引

  1. 查詢、函數、分組、排序、分頁 
  2. 添加 Insert into
  3. 編輯 Update set
  4. 刪除 Delete
  5. 生成實體
  6. 內置經常使用工具類庫  文檔完善中...
  7. 事務處理
  8. Join 鏈接查詢 1.0.2.X版本中添加 開發中...

        開源地址:https://gitee.com/wangshuyu/LambdaToSqlhtml

簡介:git

LambdaToSql 簡單的蘭姆達轉換sql,能夠直接.ToList()執行,返回查詢結果sql

 

第一個版本支持:數據庫

  1. Where 條件查詢
  2. Order by 排序
  3. Group by 分組
  4. 簡單分頁,只支持row_number
  5. 支持數據庫函數:Avg Max Min Sum Count
  6. 不須要任何第三方類庫 

 使用前置條件:緩存

  1. 主鍵必須是 Guid  uniqueidentifier  
  2. 表必須有 CreateTime datetime ,給分頁排序使用,後期會把這個條件處理掉

 

第一個版不支持:微信

  1. 暫不支持事物處理
  2. 暫不支持多表join查詢
  3. 只支持Microsoft SQL Server數據庫,分頁不支持SQL Server 2000

 

測試表Sql語句:ide

CREATE TABLE [dbo].[System_UserInfo](
    [Guid] [uniqueidentifier] NOT NULL CONSTRAINT [DF_UserInfo_Guid]  DEFAULT (newid()),
    [LoginName] [nvarchar](32) NULL,
    [UserName] [nvarchar](32) NULL,
    [PassWord] [nvarchar](64) NULL,
    [Cert] [uniqueidentifier] NULL,
    [IdCard] [nvarchar](64) NULL,
    [Birthday] [nvarchar](64) NULL,
    [Address] [nvarchar](1024) NULL,
    [ImgUrl] [nvarchar](128) NULL,
    [Gender] [nvarchar](2) NULL,
    [Mobile] [nvarchar](16) NULL,
    [Remark] [nvarchar](1024) NULL,
    [Unionid] [nvarchar](128) NULL,
    [CreateTime] [datetime] NULL CONSTRAINT [DF_UserInfo_CreaTime]  DEFAULT (getdate()),
    [IsDelete] [int] NULL,
    [CreateUserInfoID] [uniqueidentifier] NULL,
 CONSTRAINT [PK_USER] PRIMARY KEY NONCLUSTERED 
(
    [Guid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用戶ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'System_UserInfo', @level2type=N'COLUMN',@level2name=N'Guid'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'登陸名稱' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'System_UserInfo', @level2type=N'COLUMN',@level2name=N'LoginName'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用戶名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'System_UserInfo', @level2type=N'COLUMN',@level2name=N'UserName'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'密碼' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'System_UserInfo', @level2type=N'COLUMN',@level2name=N'PassWord'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'證件類型' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'System_UserInfo', @level2type=N'COLUMN',@level2name=N'Cert'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'身份證號' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'System_UserInfo', @level2type=N'COLUMN',@level2name=N'IdCard'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'出生日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'System_UserInfo', @level2type=N'COLUMN',@level2name=N'Birthday'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'地址' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'System_UserInfo', @level2type=N'COLUMN',@level2name=N'Address'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'頭像' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'System_UserInfo', @level2type=N'COLUMN',@level2name=N'ImgUrl'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'性別' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'System_UserInfo', @level2type=N'COLUMN',@level2name=N'Gender'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'手機號' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'System_UserInfo', @level2type=N'COLUMN',@level2name=N'Mobile'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'備註' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'System_UserInfo', @level2type=N'COLUMN',@level2name=N'Remark'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'微信登陸惟一碼' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'System_UserInfo', @level2type=N'COLUMN',@level2name=N'Unionid'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'建立時間' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'System_UserInfo', @level2type=N'COLUMN',@level2name=N'CreateTime'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'是否刪除' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'System_UserInfo', @level2type=N'COLUMN',@level2name=N'IsDelete'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'建立人ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'System_UserInfo', @level2type=N'COLUMN',@level2name=N'CreateUserInfoID'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用戶表' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'System_UserInfo'
GO
View Code

 

測試實體:函數

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using LambdaToSql;
using LambdaToSql.FrameWork;
using LambdaToSql.Extended;

namespace EntityModel
{

    /// <summary>
    /// 用戶表
    /// </summary>
    [Serializable]
    [DataContract]
    public class System_UserInfo
    {

        /// <summary>
        /// 用戶ID
        /// </summary>
        [DataMember]
        public System.Guid Guid { get; set; }

        /// <summary>
        /// 登陸名稱
        /// </summary>
        [DataMember]
        public string LoginName { get; set; }

        /// <summary>
        /// 用戶名
        /// </summary>
        [DataMember]
        public string UserName { get; set; }

        /// <summary>
        /// 密碼
        /// </summary>
        [DataMember]
        public string PassWord { get; set; }

        /// <summary>
        /// 證件類型
        /// </summary>
        [DataMember]
        public Nullable<System.Guid> Cert { get; set; }

        /// <summary>
        /// 身份證號
        /// </summary>
        [DataMember]
        public string IdCard { get; set; }

        /// <summary>
        /// 出生日期
        /// </summary>
        [DataMember]
        public string Birthday { get; set; }

        /// <summary>
        /// 地址
        /// </summary>
        [DataMember]
        public string Address { get; set; }

        /// <summary>
        /// 頭像
        /// </summary>
        [DataMember]
        public string ImgUrl { get; set; }

        /// <summary>
        /// 性別
        /// </summary>
        [DataMember]
        public string Gender { get; set; }

        /// <summary>
        /// 手機號
        /// </summary>
        [DataMember]
        public string Mobile { get; set; }

        /// <summary>
        /// 備註
        /// </summary>
        [DataMember]
        public string Remark { get; set; }

        /// <summary>
        /// 微信登陸惟一碼
        /// </summary>
        [DataMember]
        public string Unionid { get; set; }

        /// <summary>
        /// 建立時間
        /// </summary>
        [DataMember]
        public Nullable<System.DateTime> CreateTime { get; set; }

        /// <summary>
        /// 是否刪除
        /// </summary>
        [DataMember]
        public Nullable<int> IsDelete { get; set; }

        /// <summary>
        /// 建立人ID
        /// </summary>
        [DataMember]
        public Nullable<System.Guid> CreateUserInfoID { get; set; }
    }
}
View Code

 

測試代碼,具體查看後續文章:工具

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LambdaToSql.FrameWork;

namespace Web
{
    public partial class Clent : System.Web.UI.Page
    {
        LambdaToSql.SqlClient DB = new LambdaToSql.SqlClient();

        protected void Page_Load(object sender, EventArgs e)
        {
            //生成實體對象文件
            //LambdaToSql.SysConfig.Init();
            //LambdaToSql.Fireworks.CreateEntity.Init("d:/class/");

            //Find查詢主鍵
            //var obj = db.QueryTable<EntityModel.System_UserInfo>().Find(Guid.Parse("9c1d1247-cd2c-424d-be14-ef8abae655a4"));


            //var item = new string[] { "15842709668", "18609877687", "15804279711", "13998756128", "13019965033" }.ToList();
            //var list = db.QueryTable<EntityModel.System_UserInfo>(ex => LambdaToSql.Fireworks.Method.In(ex.LoginName, item)).ToList();
            //var list = db.QueryTable<EntityModel.System_UserInfo>(ex => item.Contains(ex.LoginName)).ToList();

            function();
            Select();
        }

        private void function() //函數查詢
        {           
            var list = DB.QueryTable<EntityModel.System_UserInfo>()
                //.Where(ex => ex.LoginName.Contains("15"))
                //.Where(ex => ex.LoginName.StartsWith("158"))
                //.Where(ex => ex.LoginName.EndsWith("88"))
                //.Where(ex => ex.LoginName.NotContains("88"))
                //.Where(ex => ex.LoginName == "Shuyu".ToLower())
                // .Where(ex => item.Contains(ex.LoginName))
                //.Where(ex => item.NotContains(ex.LoginName))
                .ToList();
        }

        private void Select()//基本查詢 分頁 
        {
            int total = 0;
            var list = DB.QueryTable<EntityModel.System_UserInfo>()
                    //.Select(ex => new { ex.LoginName, ex.UserName })
                    //.Select(ex => new { ex.PassWord, ex.IdCard })
                    //.Where(ex => ex.LoginName == "test" || ex.UserName == "社區民警")
                    //.Where(ex => ex.LoginName == "test")
                    //.GroupBy(ex => new { ex.LoginName })
                    //.GroupBy(ex => new { ex.UserName })
                    //.OrderByDescending(ex => ex.LoginName)
                    //.OrderBy(ex => ex.UserName)
                    //.Take(15)
                    //.Where(ex => ex.LoginName.Contains("15"))
                    //.Where(ex => ex.LoginName.StartsWith("158"))
                    //.Where(ex => ex.LoginName.EndsWith("88"))
                    //.Where(ex => ex.LoginName.NotContains("88"))
                    //.Where(ex => ex.LoginName == "Shuyu".ToLower())

                    //.Where(ex => Method.Like(ex.LoginName, "158%"))
                    //.Where(ex => Method.NotLike(ex.LoginName, "158%"))

                    //.Where(ex => Method.In(ex.LoginName, "15841479211"))
                    //.Where(ex => Method.In(ex.LoginName, item))
                    //.Where(ex => Method.NotIn(ex.LoginName, item))

                    //.Count();
                    //.Take(10)
                    //.Skip(2)
                    //.OrderBy(ex => ex.CreateTime)
                    //.Sum(ex => ex.LogOnCount);
                    //.Min(ex => ex.LogOnCount);
                    //.Max(ex => ex.LogOnCount);
                    //.Avg(ex => ex.LogOnCount);
                    //.First();
                    //.FirstOrDefault();
                    //.ToPageList(3, 15, ref total);
                    .ToList();
        }
    }
}
View Code

 

Web.config配置文件性能

<connectionStrings>
   <add name="ConnectionString" connectionString="Server=.;Database=Test;User ID=sa;Password=123456" providerName="System.Data.SqlClient" />
</connectionStrings>

 

 

性能:

  1. 基本和Ado.Net原生接近,lambda解析轉換成sql語句,而後使用DBHelper參數化查詢的數據庫,性能損耗是在蘭姆達轉語句上,後續版本會加緩存機制。
  2. 暫時不支持事物處理
  3. 接口 命名從新規劃,但仍是不太好,後續版本會慢慢規劃,暫時預計大概發佈 10個版本,出一個穩定版本
  4. 穩定後會開源出來 哈哈

 

如何獲取:

  1. NuGet管理器 搜索:LambdaToSql
  2. Install-Package LambdaToSql -Version 版本號

 

使用示例:

  1. 配置文件中添加連接數據庫字符串  <add name="ConnectionString" connectionString="Server=.;Database=test;User ID=sa;Password=123456" providerName="System.Data.SqlClient" />
  2. 使用實例

    DB.QueryTable<實體對象>()
    .Where(條件)
    .Select(字段)
    .GroupBy(分組)
    .OrderByDescending(排序)
    .OrderBy(排序)
    .Skip(頁碼)
    .Take(數量)
    .ToList();//返回集合

    .Count();
    .Sum(求和);
    .Min(最小值);
    .Max(最大值);
    .Avg(平均值);
    .First();//第一條
    .FirstOrDefault();//第一條
    .ToPageList(頁碼, 每頁數量, ref 總數);//分頁

  3. 其它

 最後感謝:SqlSugar 做者 開源

相關文章
相關標籤/搜索