コード例 #1
0
        public static int Delete <T>(this IDbCommand cmd, Expression <Func <T, bool> > expression)
        {
            var exp = Expre2Sql.Delete <T>().Where(expression);

            return(cmd.Execute(exp.SqlStr, exp.DbParams));
        }
コード例 #2
0
        public static int Count <T>(this IDbConnection con, Expression <Func <T, bool> > expression)
        {
            var exp = Expre2Sql.Select <T>().Where(expression);

            return(con.Execute <int>(exp.SqlStr, exp.DbParams));
        }
コード例 #3
0
        /// <summary>
        /// 查找记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="con"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static IList <T> Query <T>(this IDbCommand cmd, Expression <Func <T, bool> > expression)
        {
            var exp = Expre2Sql.Select <T>().Where(expression);

            return(cmd.Query <T>(exp.SqlStr, exp.DbParams));
        }
コード例 #4
0
        static void Main(string[] args)
        {
            Console.Title = "Expression2SqlTest";


            //通过静态属性DatabaseType或者静态方法Init均可配置数据库类型
            Expre2Sql.DatabaseType = DatabaseType.SQLServer;
            //Expre2Sql.Init(DatabaseType.SQLServer);

            Printf(
                Expre2Sql.Select <UserInfo>(),
                "查询单表所有字段"
                );

            Printf(
                Expre2Sql.Select <UserInfo>(u => u.Id),
                "查询单表单个字段"
                );

            Printf(
                Expre2Sql.Select <UserInfo>(u => new { u.Id, u.Name }),
                "查询单表多个字段"
                );


            Printf(
                Expre2Sql.Select <UserInfo>(u => u.Id).
                Where(u => u.Name.Like("b")),
                "查询单表,带where Like条件"
                );


            Printf(
                Expre2Sql.Select <UserInfo>(u => u.Id).
                Where(u => u.Name.LikeLeft("b")),
                "查询单表,带where LikeLeft条件"
                );


            Printf(
                Expre2Sql.Select <UserInfo>(u => u.Id).
                Where(u => u.Name.LikeRight("b")),
                "查询单表,带where LikeRight条件"
                );


            Printf(
                Expre2Sql.Select <UserInfo>(u => u.Name).
                Where(u => u.Id.In(1, 2, 3)),
                "查询单表,带where in条件,写法一"
                );

            int[] aryId = { 1, 2, 3 };
            Printf(
                Expre2Sql.Select <UserInfo>(u => u.Name).
                Where(u => u.Id.In(aryId)),
                "查询单表,带where in条件,写法二"
                );

            Printf(
                Expre2Sql.Select <UserInfo>(u => u.Name).
                Where(u => u.Name.In(new string[] { "a", "b" })),
                "查询单表,带where in条件,写法三"
                );


            Printf(
                Expre2Sql.Select <UserInfo>(u => u.Id).
                Where(
                    u => u.Name == "b" &&
                    u.Id > 2 &&
                    u.Name != null &&
                    u.Id > int.MinValue &&
                    u.Id < int.MaxValue &&
                    u.Id.In(1, 2, 3) &&
                    u.Name.Like("a") &&
                    u.Name.LikeLeft("b") &&
                    u.Name.LikeRight("c") ||
                    u.Id == null
                    ),
                "查询单表,带多个where条件"
                );


            Printf(
                Expre2Sql.Select <UserInfo, Account>((u, a) => new { u.Id, a.Name }).
                Join <Account>((u, a) => u.Id == a.UserId),
                "多表Join关联查询"
                );


            Printf(
                Expre2Sql.Select <UserInfo, Account>((u, a) => new { u.Id, a.Name }).
                InnerJoin <Account>((u, a) => u.Id == a.UserId),
                "多表InnerJoin关联查询"
                );


            Printf(
                Expre2Sql.Select <UserInfo, Account>((u, a) => new { u.Id, a.Name }).
                LeftJoin <Account>((u, a) => u.Id == a.UserId),
                "多表LeftJoin关联查询"
                );



            Printf(
                Expre2Sql.Select <UserInfo, Account>((u, a) => new { u.Id, a.Name }).
                RightJoin <Account>((u, a) => u.Id == a.UserId),
                "多表RightJoin关联查询"
                );


            Printf(
                Expre2Sql.Select <UserInfo, Account>((u, a) => new { u.Id, a.Name }).
                FullJoin <Account>((u, a) => u.Id == a.UserId),
                "多表FullJoin关联查询"
                );


            Printf(
                Expre2Sql.Select <UserInfo, Account, Student, Class, City, Country>((u, a, s, d, e, f) =>
                                                                                    new { u.Id, a.Name, StudentName = s.Name, ClassName = d.Name, e.CityName, CountryName = f.Name }).
                Join <Account>((u, a) => u.Id == a.UserId).
                LeftJoin <Account, Student>((a, s) => a.Id == s.AccountId).
                RightJoin <Student, Class>((s, c) => s.Id == c.UserId).
                InnerJoin <Class, City>((c, d) => c.CityId == d.Id).
                FullJoin <City, Country>((c, d) => c.CountryId == d.Id).
                Where(u => u.Id != null),
                "多表复杂关联查询"
                );


            Printf(
                Expre2Sql.Select <UserInfo>().
                GroupBy(u => u.Name),
                "GroupBy分组查询"
                );


            Printf(
                Expre2Sql.Select <UserInfo>().
                OrderBy(u => u.Id),
                "OrderBy排序"
                );



            Printf(
                Expre2Sql.Max <UserInfo>(u => u.Id),
                "返回一列中的最大值。NULL 值不包括在计算中。"
                );

            Printf(
                Expre2Sql.Min <UserInfo>(u => u.Id),
                "返回一列中的最小值。NULL 值不包括在计算中。"
                );

            Printf(
                Expre2Sql.Avg <UserInfo>(u => u.Id),
                "返回数值列的平均值。NULL 值不包括在计算中。"
                );

            Printf(
                Expre2Sql.Count <UserInfo>(),
                "返回表中的记录数"
                );

            Printf(
                Expre2Sql.Count <UserInfo>(u => u.Id),
                "返回指定列的值的数目(NULL 不计入)"
                );

            Printf(
                Expre2Sql.Sum <UserInfo>(u => u.Id),
                "返回数值列的总数(总额)。"
                );


            Printf(
                Expre2Sql.Delete <UserInfo>(),
                "全表删除"
                );

            Printf(
                Expre2Sql.Delete <UserInfo>().
                Where(u => u.Id == null),
                "根据where条件删除指定表记录"
                );


            Printf(
                Expre2Sql.Update <UserInfo>(() => new { Name = "", Sex = 1, Email = "*****@*****.**" }),
                "全表更新"
                );

            Printf(
                Expre2Sql.Update <UserInfo>(() => new { Name = "", Sex = 1, Email = "*****@*****.**" }).
                Where(u => u.Id == 1),
                "根据where条件更新指定表记录"
                );

            Console.ReadKey();
            //to be continued...
        }
コード例 #5
0
ファイル: Form1.cs プロジェクト: w312033591/orm
        public Form1()
        {
            var test = new Mock <TestFastItem>();

            test.SetupProperty(e => e.ID);


            SqlQuery <TestFastItem> context = DbContext <TestFastItem> .Get();

            foreach (var item in context)
            {
                item.ID = 1;
            }

            TestFastItem TestFastValue = new TestFastItem();
            Type         type          = TestFastValue.GetType();
            PropertyInfo propersID     = type.GetProperty("ID");
            PropertyInfo propersName   = type.GetProperty("Name");
            ///快速反射测试Emit实现
            SetValueDelegate delegateID   = FastMethodFactory.CreatePropertySetter(propersID);
            SetValueDelegate delegateName = FastMethodFactory.CreatePropertySetter(propersName);

            delegateID(TestFastValue, 4);
            delegateName(TestFastValue, "aaa");
            //快速反射测试Delegate.CreateDelegate实现
            Action <TestFastItem, int> setter = (Action <TestFastItem, int>)Delegate.CreateDelegate(
                typeof(Action <TestFastItem, int>), null, propersID.GetSetMethod());

            Student t1 = new Student();;

            t1.Age = 20;
            //动态类型
            dynamic d = t1;

            d = t1;
            Expre2Sql.DatabaseType = Orm.Config.DataSourceType.MysSql;
            //简单查询
            var    sudent1 = Expre2Sql.Select <Student>();
            string str     = sudent1.SqlStr;
            Int32  a       = 2147483647;

            //溢出检测
            checked {
                a = a - 1;
            }
            a = unchecked (a + 1);
            bool b = a.Equals(111);
            //查询单字段
            var sudent2 = Expre2Sql.Select <Student>(u => u.Age);

            str = sudent2.SqlStr;
            //查询多个字段
            var sudent3 = Expre2Sql.Select <Student>(u => new {
                u.Age,
                u.Address
            });
            //"查询单表,带where Like条件"
            var sudent4 = Expre2Sql.Select <Student>(u => u.Age).
                          Where(u => u.Name.Like("b"));

            str = sudent4.SqlStr;
            str = sudent1.GetHashCode().ToString();
        }
コード例 #6
0
ファイル: DbContext.cs プロジェクト: w312033591/orm
        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        public static SqlQuery <TEntity> Get()
        {
            SqlQuery <TEntity> entity = new SqlQuery <TEntity>(Expre2Sql.Select <TEntity>());

            return(entity);
        }