Beispiel #1
0
 public void JoinLeft <T1, T2>(Expression <Func <T1, T2, bool> > joinExpression, params Expression <Func <T1, T2, object> >[] fields)
 {
     string strWhere = LinqCompileExt.GetJoinByLambda(joinExpression, DataBaseType.SqlServer);
     Type   t1       = typeof(T1); //获得该类的Type
     Type   t2       = typeof(T2); //获得该类的Type
     //GetTable(joinExpression.Parameters, t1, t2);
 }
        private static string NotExists <T, SubT>(Expression <Func <T, SubT, bool> > notExistsFilter = null)
        {
            var    model    = Activator.CreateInstance <SubT>();
            Type   t        = model.GetType(); //获得该类的Type
            string tbName   = GetTableName(t); //表名
            string nickName = " as " + notExistsFilter.Parameters[1];
            string filter   = LinqCompileExt.GetJoinByLambda(notExistsFilter, DataBaseType.SqlServer);
            string strSql   = string.Format(" not exists (select * from {0} {2} where {1}) ", tbName, filter, nickName);

            return(strSql);
        }
Beispiel #3
0
        /// <summary>
        /// 多表联合查询(left join)
        /// </summary>
        /// <typeparam name="T">返回的数据实体类型</typeparam>
        /// <typeparam name="T1">表1</typeparam>
        /// <typeparam name="T2">表2</typeparam>
        /// <param name="joinExpression">联接条件</param>
        /// <param name="filter">查询条件</param>
        /// <param name="order">排序</param>
        /// <param name="fields">查询字段</param>
        /// <returns>数据实体列表</returns>
        protected static string JoinScript <T, T1, T2>(Expression <Func <T1, T2, bool> > joinExpression, Expression <Func <T1, T2, bool> > filter = null, OrderBy <T> order = null,
                                                       params Expression <Func <T1, T2, object> >[] fields)
        {
            string join      = string.Empty;
            string strWhere  = string.Empty;
            string strFields = string.Empty;
            string orderBy   = string.Empty;

            join = LinqCompileExt.GetJoinByLambda(joinExpression, DataBaseType.SqlServer);

            if (filter != null)
            {
                strWhere = LinqCompileExt.GetJoinByLambda(filter, DataBaseType.SqlServer);
            }

            if (order != null)
            {
                orderBy = OrderByUtil.GetOrderBy <T>(order.GetOrderByList());
            }

            foreach (var f in fields)
            {
                var fieldName = ExpressionField.GetFieldName <T1, T2>(f);
                strFields = strFields + "," + fieldName;
            }
            if (string.IsNullOrEmpty(strFields))
            {
                strFields = "*";
            }
            else
            {
                strFields = strFields.Trim(',');
            }

            StringBuilder strSql = new StringBuilder();
            var           model  = Activator.CreateInstance <T>();

            Type[] agrs   = new Type[] { typeof(T1), typeof(T2) };      //获得该类的Type
            string tbName = JoinTable(joinExpression.Parameters, agrs); //表名

            strSql.AppendFormat("select {0} ", strFields);
            strSql.AppendFormat(" from {0} ", tbName);
            strSql.AppendFormat(" on {0}", join);
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql.AppendFormat(" where {0} ", strWhere);
            }
            if (!string.IsNullOrEmpty(orderBy))
            {
                strSql.Append(orderBy);
            }

            return(strSql.ToString());
        }