using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OrmTest { public partial class NewUnitTest { public static Unit_SYS_USER UserLoginInfo => new Unit_SYS_USER() { XH = "10010" }; public static void Queryable() { var pageindex = 1; var pagesize = 10; var total = 0; var totalPage = 0; var list = Db.Queryable().ToPageList(pageindex, pagesize, ref total, ref totalPage); //Db.CodeFirst.InitTables(typeof(CarType)); //Db.Updateable() // .SetColumns(it => new CarType { State = SqlSugar.SqlFunc.IIF(it.State == true, false, true) }).Where(it => true) // .ExecuteCommand(); //Db.CodeFirst.InitTables(typeof(TestTree)); //Db.DbMaintenance.TruncateTable(); //Db.Ado.ExecuteCommand("insert testtree values(hierarchyid::GetRoot(),geography :: STGeomFromText ('POINT(55.9271035250276 -3.29431266523898)',4326),'name')"); //var list2 = Db.Queryable().ToList(); Db.CodeFirst.InitTables(); Db.Queryable().Where(it => it.Id.HasValue).ToList(); Db.Queryable().Where(it => SqlSugar.SqlFunc.Equals(it.CreateTime.Date, it.CreateTime.Date)).ToList(); var sql = Db.Queryable().Select(it => new UnitSelectTest() { DcNull = it.Dc, Dc = it.Int }).ToSql().Key; UValidate.Check(sql, "SELECT [Dc] AS [DcNull] , [Int] AS [Dc] FROM [UnitSelectTest]", "Queryable"); sql = Db.Updateable(new UnitSelectTest2()).ToSql().Key; UValidate.Check(sql, @"UPDATE [UnitSelectTest2] SET [Dc]=@Dc,[IntNull]=@IntNull WHERE [Int]=@Int", "Queryable"); sql = Db.Queryable().IgnoreColumns(it => it.CreateTime).ToSql().Key; UValidate.Check(sql, "SELECT [Id],[Name],[Price],[CustomId] FROM [Order] ", "Queryable"); sql = Db.Queryable().IgnoreColumns(it => new { it.Id, it.Name }).ToSql().Key; UValidate.Check(sql, "SELECT [Price],[CreateTime],[CustomId] FROM [Order] ", "Queryable"); sql = Db.Queryable().IgnoreColumns("id").ToSql().Key; UValidate.Check(sql, "SELECT [Name],[Price],[CreateTime],[CustomId] FROM [Order] ", "Queryable"); var cts = IEnumerbleContains.Data(); var list2 = Db.Queryable() .Where(p => /*ids.*/cts.Select(c => c.Id).Contains(p.Id)).ToList(); var cts2 = IEnumerbleContains.Data().ToList(); ; var list3 = Db.Queryable() .Where(p => /*ids.*/cts2.Select(c => c.Id).Contains(p.Id)).ToList(); var list4 = Db.Queryable() .Where(p => new List { 1, 2, 3 }.Where(b => b > 1).Contains(p.Id)).ToList(); Db.CodeFirst.InitTables(); var list5 = Db.Queryable().Where(it => SqlSugar.SqlFunc.ToString(it.Date.Value.Year) == "1").ToList(); var list6 = Db.Queryable().Where(it => it.Date.Value.Year == 1).ToList(); var list7 = Db.Queryable().Where(it => it.Date.Value.Date == DateTime.Now.Date).ToList(); SaleOrder saleOrderInfo = new SaleOrder(); Db.CodeFirst.InitTables(); var result = Db.GetSimpleClient().Update(o => new SaleOrder() { OrderStatus = 1, CheckMan = saleOrderInfo.CheckMan, CheckTime = DateTime.Now }, o => o.OrderSn == saleOrderInfo.OrderSn && o.OrderStatus != 1); var ids = Enumerable.Range(1, 11).ToList(); var list8 = Db.Queryable().Where(it => SqlFunc.ContainsArrayUseSqlParameters(ids, it.Id)).ToList(); var result2 = Db.Queryable().Where(o => o.XH == UserLoginInfo.XH).Select(o => o.XH).ToSql(); var x = Db.Queryable().Select(it => new BoolTest2() { a = it.a }).ToSql(); UValidate.Check(x.Key, "SELECT [a] AS [a] FROM [BoolTest1] ", "Queryable"); x = Db.Queryable().Select(it => new BoolTest1() { a = it.a.Value }).ToSql(); UValidate.Check(x.Key, "SELECT [a] AS [a] FROM [BoolTest2] ", "Queryable"); Db.CodeFirst.InitTables(); var blist3= Db.Queryable().Select(it => new BoolTest3() { a = string.IsNullOrEmpty(it.Name) }).ToList(); var blist4 = Db.Queryable().Select(it => new BoolTest3() { a = SqlFunc.IIF(it.a == true, true, false) }).ToList(); var blist5 = Db.Queryable().Select(it => new BoolTest3() { a = SqlFunc.IF(it.a == true).Return(true).End(false) }).ToList(); var blist6 = Db.Queryable().Select(it => new BoolTest3() { a =it.a==true?true:false }).ToList(); var blist7 = Db.Queryable().Select(it => new BoolTest3() { a = SqlFunc.Subqueryable().Any() }).ToList(); var db = Db; db.CodeFirst.InitTables(); db.Deleteable().ExecuteCommand(); db.Deleteable().ExecuteCommand(); db.Insertable(new UserInfo() { Id = 1, Password = "123", UserName = "admin" }).ExecuteCommand(); db.Insertable(new UserIpRuleInfo() { Addtime = DateTime.Now, UserName = "a", Id = 11, UserId = 1, Description = "xx", IpRange = "1", RuleType = 1 }).ExecuteCommand(); var vmList = db.Queryable( (m1, m2) => m1.Id == m2.UserId ).Where((m1, m2) => m1.Id > 0).Select((m1, m2) => new UserIpRuleInfo() { IpRange = m2.IpRange, Addtime = m2.Addtime, RuleType = m2.RuleType, }).ToList(); if (string.IsNullOrEmpty(vmList.First().IpRange)) { throw new Exception("Queryable"); } Db.Insertable(new Order() { CreateTime=DateTime.Now, CustomId=1, Name="a",Price=1 }).ExecuteCommand(); var sa = Db.SqlQueryable("SELECT * FroM [ORDER] where id in (@id) "); sa.AddParameters(new List() { new SugarParameter("id",new int[]{ 1}) }); int i = 0; var salist= sa.ToPageList(1,2,ref i); db.CodeFirst.InitTables(); db.Insertable(new UnitBytes11() { bytes = null, name = "a" }).ExecuteCommand(); db.Insertable(new UnitBytes11() { bytes=new byte[] { 1,2} , name="a"}).ExecuteCommand(); var bytes = db.Queryable().Select(it => new { b = it.bytes, name="a" }).ToList(); } public class UnitBytes11 { [SugarColumn(Length =200,IsNullable =true)] public byte[] bytes { get; set; } public string name{ get; set; } } /// /// /// [Serializable] [SugarTable("users")] public class UserInfo { /// /// /// public int Id { get; set; } /// /// /// public string UserName { get; set; } /// /// /// public string Password { get; set; } } /// /// /// [Serializable] [SugarTable("user_ip_rules")] public class UserIpRuleInfo { /// /// 自增Id /// public int Id { get; set; } /// /// 用户Id /// [SugarColumn(ColumnName = "user_id")] public int UserId { get; set; } /// /// 用户名 /// [SugarColumn(IsIgnore = true)] public string UserName { get; set; } /// /// IP地址或范围 /// [SugarColumn(ColumnName = "ip_range")] public string IpRange { get; set; } /// /// 规则类型 0-黑名单 1-白名单 /// [SugarColumn(ColumnName = "rule_type")] public int RuleType { get; set; } /// /// 描述/备注 /// public string Description { get; set; } /// /// 添加时间 /// public DateTime Addtime { get; set; } } /// /// 系统用户表实体模型类 /// [SugarTable("Unit_SYS_USER")] public class Unit_SYS_USER { /// /// 序号 /// private string _XH; /// /// 序号【主键唯一标识,自动生成】 /// [SugarColumn(ColumnName = "XH", ColumnDataType = "VARCHAR2", IsPrimaryKey = true, IsNullable = false, Length = 50, ColumnDescription = "序号【主键唯一标识,自动生成】")] public string XH { get { return _XH; } set { _XH = value; } } } public static class IEnumerbleContains { public static IEnumerable Data() { for (int i = 0; i < 100; i++) { yield return new Order { Id = i, }; } } } [SugarTable("UnitSaleOrder")] public class SaleOrder { public SaleOrder() { SaleDate = DateTime.Now; Team = 1; AddTime = DateTime.Now; OrderStatus = 0; Points = 0; PayPoints = 0; PointsExchangeMoney = decimal.Zero; IsPushMessage = false; CostAmount = decimal.Zero; OrderAmount = decimal.Zero; RealOrderAmount = decimal.Zero; AccountsDueAmount = decimal.Zero; SettleType = 0; IsPushMessage = false; } /// /// 订单号 /// public string OrderSn { get; set; } /// /// 客户编号 /// public string CustomerNo { get; set; } /// /// 收货人姓名 /// public string CustomerName { get; set; } /// /// 成本总金额 /// public decimal CostAmount { get; set; } /// /// 订单总金额 /// public decimal OrderAmount { get; set; } /// /// 实收金额(整单优惠后) /// public decimal RealOrderAmount { get; set; } /// /// 销货日期 /// public DateTime SaleDate { get; set; } /// /// 下单时间 /// public DateTime AddTime { get; set; } /// /// 媒体资源投放ID /// public string IndustryCode { get; set; } public string IndustryName { get; set; } /// /// 备注 /// public string Remark { get; set; } /// /// 班组 /// public int Team { get; set; } /// /// 销售员编号 /// public string SellerNo { get; set; } /// /// 销售员姓名 /// public string SellerName { get; set; } /// /// 操作人ID /// public virtual string HandlerCode { get; set; } /// /// 操作者 /// public string Handler { get; set; } /// /// 发货仓库代号 /// public string StoreCode { get; set; } /// /// 发货仓库名称 /// public string StoreName { get; set; } /// /// 销货店铺渠道代号 /// public string ShopChannelCode { get; set; } /// /// 销货店铺渠道名称 /// public string ShopChannelName { get; set; } /// /// 订单产品数 /// public int GoodsNum { get; set; } /// /// 礼品数量 /// public int GiftNum { get; set; } /// /// 对应预订单号 /// public string CustomerOrderSn { get; set; } /// /// 订单赠送积分 /// public int Points { get; set; } /// /// 应收款金额 /// public decimal AccountsDueAmount { get; set; } /// /// 来自预约单号 /// public string ReserationOrderSn { get; set; } /// /// 订单状态 0为未审核 1为已审核 /// public int OrderStatus { get; set; } /// /// 审核人 /// public string CheckMan { get; set; } /// /// 审核时间 /// public DateTime? CheckTime { get; set; } /// /// 结算类型 0为非金工石(零售) 1为金工石 /// public int SettleType { get; set; } /// /// 使用积分 /// public int PayPoints { get; set; } /// /// 积分抵现金额 /// public decimal PointsExchangeMoney { get; set; } /// /// 是否已推送微信消息 /// public bool IsPushMessage { get; set; } } public class SaleOrderBaseInfo { public int GoodsNum { get; set; } public int GiftNum { get; set; } public decimal OrderAmount { get; set; } } public class UnitTest3 { public DateTime? Date { get; set; } } public class UnitSelectTest2 { [SqlSugar.SugarColumn(IsOnlyIgnoreUpdate = true)] public decimal? DcNull { get; set; } public decimal Dc { get; set; } public int? IntNull { get; set; } [SqlSugar.SugarColumn(IsPrimaryKey = true)] public decimal Int { get; set; } } public class UnitSelectTest { public decimal? DcNull { get; set; } public decimal Dc { get; set; } public int? IntNull { get; set; } public decimal Int { get; set; } } public class UnitGuidTable { public Guid? Id { get; set; } } } internal class BoolTest1 { public BoolTest1() { } public bool a { get; set; } } public class BoolTest2 { public bool? a { get; set; } } public class BoolTest3 { public bool? a { get; set; } public string Name { get; set; } } }