Example #1
0
        static void Main(string[] args)
        {
            EntityCompany company = new EntityCompany();

            company.Name        = "asdf";
            company.Number      = "1ads23";
            company.CreatedDate = DateTime.Now;

            SqlHelper sh = new SqlHelper();
            //long id = sh.Insert<EntityCompany>(company);

            ConvertToSqlExpressionVisitor visitor = new ConvertToSqlExpressionVisitor();
            //List<long> ids = new List<long>();
            //ids.Add(3);
            //ids.Add(4);
            //ids.Add(5);
            //Expression<Func<EntityCompany, bool>> expression = c => (c.Id > 1
            //            && c.Id < 10
            //            || c.Id == 5)
            //            && c.Number.StartsWith("1")
            //            && (c.Number.EndsWith("2")
            //            || c.Number.Contains("3"))
            //            && ids.Contains(c.Id);

            Expression <Func <EntityCompany, bool> > expression = r => r.Id > 2 && r.Id < 10 && r.Number.Contains("1");
            var list = sh.Find <EntityCompany>(expression);

            Console.WriteLine(list.Count);

            Console.Read();
        }
Example #2
0
        public List <T> Find <T>(Expression <Func <T, bool> > lambda) where T : EntityBase, new()
        {
            Type   type = typeof(T);
            string sql  = SqlCacheBuilder <T> .GetSql(SqlCacheBuilderType.FindAll);

            ConvertToSqlExpressionVisitor visitor = new ConvertToSqlExpressionVisitor();

            visitor.Visit(lambda);
            string where = visitor.GetWhere();
            sql         += where;
            return(this.ExecuteSql <List <T> >(sql, null, cmd =>
            {
                List <T> list = new List <T>();
                var reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    T t = new T();
                    foreach (var item in type.GetProperties())
                    {
                        string propertyName = item.GetMappingName();
                        item.SetValue(t, reader[propertyName] is DBNull ? null : reader[propertyName]);
                    }
                    list.Add(t);
                }
                return list;
            }, SqlConnectionPool.GetConnectionString(DBOPerateType.Write)));
        }