• LINQ 多条件写法


    源代码:

    string depAll = (ddl_dep1.SelectedValue == "") ? "" : ddl_dep1.SelectedValue + '%';
            string dep = ddl_dep1.SelectedValue.Split('/')[1].ToString();
            string strSql_dep = "Select * from dep where DEP_CLASS=2 AND dep_all_code LIKE '" + depAll + "' AND dep_code in (SELECT DISTINCT DEP2 FROM depview WHERE dep3 in (SELECT dep_code FROM person_limits WHERE upper(account)='" + UserID.ToUpper() + "')  OR dep2 in (SELECT dep_code FROM person_limits WHERE upper(account)='" + UserID.ToUpper() + "')  )Order By LDAP";
     if (Request["account"] == null)
            {
                strSql_dep = strSql_dep.Replace("SELECT dep_code FROM person_limits", "SELECT user_dep FROM users");
    }
    DataTable dt_dep2 = cOracle.ProduceData(strSql_dep);
    DataTable dtCount = cOracle.ProduceData("SELECT COUNT(*) FROM depview WHERE dep3 in (SELECT dep_code FROM person_limits WHERE upper(account)='" + UserID + "') AND dep1='" + dep + "' UNION all SELECT COUNT(*) FROM depview d WHERE dep1='" + dep + "'");
    }
            if (dtCount.Rows.Count == 2)
            {
                if (dtCount.Rows[0][0].ToString() == dtCount.Rows[1][0].ToString())
                {
                    ddl_dep2.Items.Add(new ListItem(base.GetLocalResourceObject("PleaseSelect").ToString(), ""));
                }
            }
    foreach (DataRow dr in dt_dep2.Rows)
            {
                ddl_dep2.Items.Add(new ListItem(dr["DEP_NAME"].ToString().Trim(), dr["DEP_ALL_CODE"].ToString().Trim()));
            }

    重写为LINQ:

    string strSql_dep = "Select * from dep where DEP_CLASS=2 AND dep_all_code LIKE '" + depAll + "' AND dep_code in (SELECT DISTINCT DEP2 FROM depview WHERE dep3 in (SELECT dep_code FROM person_limits WHERE upper(account)='" + UserID.ToUpper() + "')  OR dep2 in (SELECT dep_code FROM person_limits WHERE upper(account)='" + UserID.ToUpper() + "')  )Order By LDAP";
            // Xudaxia:修改EF
            depAll = (ddl_dep1.SelectedValue == "") ? "" : ddl_dep1.SelectedValue;
            IPERSON_LIMITSRepository personLimRep = new PERSON_LIMITSRepository(DatabaseFactory.GetFactory());
            IDEPVIEWRepository depViewRep = new DEPVIEWRepository(DatabaseFactory.GetFactory());
            IDEPRepository depRepository = new DEPRepository(DatabaseFactory.GetFactory());
            IEnumerable<string> dep_codes = from p in personLimRep.GetAll()
                                            where p.ACCOUNT.ToUpper() == UserID.ToUpper()
                                            select p.DEP_CODE;
            IEnumerable<string> DEP2s = (from d in depViewRep.GetAll()
                                         where dep_codes.Contains(d.DEP3) ||
                                         dep_codes.Contains(d.DEP2)
                                         select d.DEP2).Distinct();
            var query = from d in depRepository.GetAll()
                        where d.DEP_CLASS == 2 &&
                        d.DEP_ALL_CODE.StartsWith(depAll) &&
                        DEP2s.Contains(d.DEP_CODE)
                        select d;
    
            if (Request["account"] == null)
            {
                strSql_dep = strSql_dep.Replace("SELECT dep_code FROM person_limits", "SELECT user_dep FROM users");
                // Xudaxia:修改EF
                IUSERSRepository userRep = new USERSRepository(DatabaseFactory.GetFactory());
                dep_codes = from u in userRep.GetAll()
                            where u.ACCOUNT.ToUpper() == UserID.ToUpper()
                            select u.USER_DEP;
                DEP2s = (from d in depViewRep.GetAll()
                         where dep_codes.Contains(d.DEP3) ||
                         dep_codes.Contains(d.DEP2)
                         select d.DEP2).Distinct();
                query = from d in depRepository.GetAll()
                        where d.DEP_CLASS == 2 &&
                        d.DEP_ALL_CODE.StartsWith(depAll) &&
                        DEP2s.Contains(d.DEP_CODE)
                        select d;
            }
            //DataTable dt_dep2 = cOracle.ProduceData(strSql_dep);
            // Xudaxia:修改EF
            DataTable dt_dep2 = query.ToDataTable();
            //DataTable dtCount = cOracle.ProduceData("SELECT COUNT(*) FROM depview WHERE dep3 in (SELECT dep_code FROM person_limits WHERE upper(account)='" + UserID + "') AND dep1='" + dep + "' UNION all SELECT COUNT(*) FROM depview d WHERE dep1='" + dep + "'");
    
            dep_codes = from p in personLimRep.GetAll()
                        where p.ACCOUNT.ToUpper() == UserID
                        select p.DEP_CODE;
            int count1 = (from d in depViewRep.GetAll()
                          where dep_codes.Contains(d.DEP3) &&
                          d.DEP1 == dep
                          select d).Count();
            int count2 = depViewRep.GetMany(d => d.DEP1 == dep).Count();
            //if (dtCount.Rows.Count == 2)
            //{
            //    if (dtCount.Rows[0][0].ToString() == dtCount.Rows[1][0].ToString())
            //    {
            //        ddl_dep2.Items.Add(new ListItem(base.GetLocalResourceObject("PleaseSelect").ToString(), ""));
            //    }
            //}
            if (count1 == count2)
            {
                ddl_dep2.Items.Add(new ListItem(base.GetLocalResourceObject("PleaseSelect").ToString(), ""));
            }
            foreach (DataRow dr in dt_dep2.Rows)
            {
                ddl_dep2.Items.Add(new ListItem(dr["DEP_NAME"].ToString().Trim(), dr["DEP_ALL_CODE"].ToString().Trim()));
            }
  • 相关阅读:
    需求管理是CMM可重复级中的6个关键过程域之一,其主要目标是__________。A.客观地验证需求管理活动
    47、软件需求工程的活动可以划分为5个独立的阶段:需求获取、需求建模、形成需求规格、需求验证和需求管理,需求建模是()
    JavaScript alert()函数
    系统讲解一下,Dao,Entity,Servlet,Action各自有什么东西-Java/Web开发
    用Eclipse 开发Dynamic Web Project应用程序
    Connection conn = DriverManager.getConnection("jdbc:odbc:bbs");
    linux 常用快捷命令
    Docker 命令大全
    小白入门之十七:yum源配置并使用其安装软件包
    Linux从一般用户切换到root用户
  • 原文地址:https://www.cnblogs.com/byvar/p/3765916.html
Copyright © 2020-2023  润新知