封装SQL的In查询的代码-WhereAddSqlIn8ListLong

2017-06-21 14:51:58  访问(1217) 赞(0) 踩(0)

  •         /// <summary>
            /// Where的Str in/not in查询
            /// </summary>
            /// <param name="info">数据库表字段</param>
            /// <param name="isIn">是否in查询</param>
            /// <param name="theList">查询值</param>
            public void WhereAddSqlIn8ListLong
                (
                    EntityFieldInfo info,
                    bool isIn,
                    List<long> theList
                )
            {
                if (theList == null || theList.Count == 0)
                {
    
                    throw new Exception
                        (
                            "方法:"
                            + MethodBase.GetCurrentMethod().ReflectedType.FullName
                            + " "
                            + MethodBase.GetCurrentMethod().ToString()
                            + " 发生异常:"
                            + "传入的字符串参数:"
                            + "List<int> theList"
                            + "为null或为空。"
                        );
                }
    
                string theValue = null;
    
                int iCount = theList.Count;
    
                // 指定个数内的in查询 //
                if (iCount <= MaxSqlInNum)
                {
                    switch (iCount)
                    {
                        case 1:
                            theValue = theList[0].ToString();
                            break;
                        case 2:
                            theValue = theList[0].ToString() + "," + theList[1].ToString();
                            break;
                        default:
    
                            StringBuilder sb = new StringBuilder();
                            sb.Append(theList[0].ToString());
    
                            for (int i = 1; i < iCount; ++i)
                            {
                                sb.Append("," + theList[i].ToString());
                            }
                            theValue = sb.ToString();
                            break;
                    }
    
    
                    m_ListIQueryItem.Add
                       (
                           new DataColumnQuerySqlInItem
                               (
                                    info,
                                    isIn,
                                    theValue
                               )
                       );
    
                    return;
                }
    
                // 超过指定个数的in查询 //
                List<string> listStr
                    = 
                    WhereAddSqlIn6ListLong2Str(theList);
    
                if (listStr == null || listStr.Count == 0)
                {
                    throw new Exception
                        (
                            "方法:"
                            + MethodBase.GetCurrentMethod().ReflectedType.FullName
                            + " "
                            + MethodBase.GetCurrentMethod().ToString()
                            + " 发生异常:"
                            + "传入的字符串参数:"
                            + "List<int> theList"
                            + "为null或为空。"
                        );
                }
    
                int listCount = listStr.Count;
    
                if (listCount == 1)
                {
                    m_ListIQueryItem.Add
                      (
                          new DataColumnQuerySqlInItem
                              (
                                   info,
                                   isIn,
                                   listStr[0]
                              )
                      );
                }
                else
                {
                    GroupQueryItem gqi 
                        = 
                        new GroupQueryItem(Enums.SqlConditionLink.EmSqlConditionLink.and);
    
                    for (int i = 0; i < listCount; ++i)
                    {
                        gqi.WhereAddSqlIn
                            (
                                SlowX.Core.Enums.SqlConditionLink.EmSqlConditionLink.or,
                                info,
                                isIn,
                                listStr[i]
                            );
    
                    }
    
                    AddGroupQueryItem(gqi);
                }
    
            }
    
    
  • 
            /// <summary>
            /// Sql 的 in 查询,把list-long 改成 list - str 模式,以便可以做分组in查询
            /// </summary>
            /// <param name="theList"></param>
            /// <returns></returns>
            protected List<string> WhereAddSqlIn6ListLong2Str
                (
                    List<long> theList
                )
            {
                int iCount = theList.Count;
    
                if (iCount == 0)
                {
    
                    throw new Exception
                        (
                            "方法:"
                            + MethodBase.GetCurrentMethod().ReflectedType.FullName
                            + " "
                            + MethodBase.GetCurrentMethod().ToString()
                            + " 发生异常:"
                            + "传入的字符串参数:"
                            + "List<int> theList"
                            + "为null或为空。"
                        );
                }
    
                List<string> theResult = new List<string>();
    
                string theValue = null;
                int pageSize = MaxSqlInNum;
    
                if (iCount <= pageSize)
                {
                    switch (iCount)
                    {
                        case 1:
                            theValue = theList[0].ToString();
                            break;
                        case 2:
                            theValue = theList[0].ToString() + "," + theList[1].ToString();
                            break;
                        default:
    
                            StringBuilder sb = new StringBuilder();
                            sb.Append(theList[0].ToString());
    
                            for (int i = 1; i < iCount; ++i)
                            {
                                sb.Append("," + theList[i].ToString());
                            }
                            theValue = sb.ToString();
                            break;
                    }
    
                    theResult.Add(theValue);
    
                    return theResult;
                }
    
                StringBuilder sbIds = null;
                bool isFirst = true;
                int addIdx = 0;
    
                for (int i = 0; i < iCount; ++i)
                {
                    if (isFirst)
                    {
                        sbIds = new StringBuilder();
                        isFirst = false;
                    }
                    else {
                        sbIds.Append(",");
                    }
    
    
                    sbIds.Append(theList[i]);
    
    
                    ++addIdx;
    
                    if (addIdx == pageSize)
                    {
                        addIdx = 0;
                        isFirst = true;
                        theResult.Add(sbIds.ToString());
    
                        // 主动销毁资源,虽然意义不大 //
                        sbIds = null;
                    }
    
                }
    
                if (!isFirst)
                {
                    if (sbIds != null)
                    {
                        theResult.Add(sbIds.ToString());
                        sbIds = null;
                    }
    
                }
    
                return theResult;
            }
    
    

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)
 
  ┈全部┈  
 
(显示默认分类)