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()));
        }

 

 

posted @ 2014-06-03 15:53  许大虾  阅读(782)  评论(0编辑  收藏  举报