Example #1
0
        /// <summary>
        /// 根据主键来取记录(最常用)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connInfo"></param>
        /// <param name="PkValues"></param>
        /// <returns></returns>
        public static T SelectPrimitive <T>(this DbContext connInfo, object[] pkValues, IEnumerable <string> onlyFields = null, bool isExceptField = false, string tableName = "", bool forUpdate = false)
            where T : new()
        {
            string sql = "";

            if (isExceptField && 0 < (onlyFields?.Count() ?? 0))//排除字段
            {
                sql = connInfo.GetSelectSql <T>(false, GetFields(typeof(T), true, onlyFields.ToArray()), tableName);
            }
            else
            {
                sql = connInfo.GetSelectSql <T>(false, onlyFields, tableName);
            }
            if (forUpdate && connInfo.SupportForUpdate)
            {
                sql += " for update";
            }
            TableInfo   ti    = GetTableInfo(typeof(T), tableName);
            RequestBase param = new RequestBase();
            int         j     = 0;

            foreach (string pkName in ti.PKeys)
            {
                param.Add(pkName, pkValues[j]);
                ++j;
            }
            return(connInfo.Db.QueryFirstOrDefault <T>(sql, param, connInfo.Transaction, commandType: CommandType.Text));
        }
Example #2
0
        public static IEnumerable <T> SelectPrimitive <T>(this DbContext connInfo, RequestBase param, IEnumerable <string> onlyFields, bool isExceptField = false, string tableName = "")
            where T : new()
        {
            string sql = "";

            if (isExceptField && 0 < (onlyFields?.Count() ?? 0))//排除字段
            {
                sql = connInfo.GetSelectSql <T>(param, GetFields(typeof(T), true, onlyFields.ToArray()), tableName);
            }
            else
            {
                sql = connInfo.GetSelectSql <T>(param, onlyFields, tableName);
            }
            return(connInfo.Db.Query <T>(sql, param, connInfo.Transaction, commandType: CommandType.Text));
        }
Example #3
0
 public static IEnumerable <T> SelectAll <T>(this DbContext connInfo, string tableName = "")
     where T : new()
 {
     return(connInfo.Db.Query <T>(connInfo.GetSelectSql <T>(true), transaction: connInfo.Transaction, commandType: CommandType.Text));
 }