開源地址:https://gitee.com/wangshuyu/LambdaToSqlhtml
簡介:git
LambdaToSql 簡單的蘭姆達轉換sql,能夠直接.ToList()執行,返回查詢結果sql
第一個版本支持:數據庫
使用前置條件:緩存
第一個版不支持:微信
測試表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
測試實體:函數
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; } } }
測試代碼,具體查看後續文章:工具
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(); } } }
Web.config配置文件性能
<connectionStrings> <add name="ConnectionString" connectionString="Server=.;Database=Test;User ID=sa;Password=123456" providerName="System.Data.SqlClient" /> </connectionStrings>
性能:
如何獲取:
使用示例:
使用實例
DB.QueryTable<實體對象>()
.Where(條件)
.Select(字段)
.GroupBy(分組)
.OrderByDescending(排序)
.OrderBy(排序)
.Skip(頁碼)
.Take(數量)
.ToList();//返回集合
.Count();
.Sum(求和);
.Min(最小值);
.Max(最大值);
.Avg(平均值);
.First();//第一條
.FirstOrDefault();//第一條
.ToPageList(頁碼, 每頁數量, ref 總數);//分頁
最後感謝:SqlSugar 做者 開源