Example #1
0
        public void CodeUsingSharpQL()
        {
            var u = new UserTable();
            var p = new PostTable();

            var q = new SharpQuery();
            var condition = Predicate.TRUE;
            if (string.IsNullOrWhiteSpace(this.Email)) {
                condition &= u.Email.IsEqualTo(this.Email);
            }

            if (this.Date.HasValue) {
                condition &= p.PostDate.IsGreaterThanOrEqualTo(this.Date);
            }

            q.Select(p.Id, p.Title, p.Text, p.PostDate, u.Email.As("UserMail"))
                .From(p)
                .InnerJoin(u, p.UserId.IsEqualTo(u.Id))
                .Where(condition)
                .EndStatement()
                .OrderByDesc(p.PostDate)
                .EndStatement();

            using (var connection = new SqlConnection("connectionString")) {
                var cmd = q.CreateCommand(connection);
                using (var reader = cmd.ExecuteReader()) {
                    // code to read data goes here
                }
            }
        }
Example #2
0
        // You know for sure that the previous code sample is very error prone.
        // It is hard to understand and hard to extend. If you need to change it you'll most likely break something and you'll find out in runtime instead of compile time.
        //
        // Here is the equivalent code written in SharpQL

        #endregion

        #region ns code

        public void CodeUsingSharpQL()
        {
            var u = new UserTable();
            var p = new PostTable();

            var q         = new SharpQuery();
            var condition = Predicate.TRUE;

            if (string.IsNullOrWhiteSpace(this.Email))
            {
                condition &= u.Email.IsEqualTo(this.Email);
            }

            if (this.Date.HasValue)
            {
                condition &= p.PostDate.IsGreaterThanOrEqualTo(this.Date);
            }

            q.Select(p.Id, p.Title, p.Text, p.PostDate, u.Email.As("UserMail"))
            .From(p)
            .InnerJoin(u, p.UserId.IsEqualTo(u.Id))
            .Where(condition)
            .EndStatement()
            .OrderByDesc(p.PostDate)
            .EndStatement();

            using (var connection = new SqlConnection("connectionString")) {
                var cmd = q.CreateCommand(connection);
                using (var reader = cmd.ExecuteReader()) {
                    // code to read data goes here
                }
            }
        }