通过一组规则过滤Iqueryable对象

时间:2017-11-28 21:45:02

标签: c# asp.net .net asp.net-mvc linq-to-sql

我已经通过一些规则过滤了一个Iqueryable对象,使用连续的Where子句做&&过滤,我怎么能为||做同样的事情过滤? 有人有建议吗?

使用其他词语,我想申请所有规则||过滤

我将展示一部分代码来举例说明我要求的内容。

我获取页面对象:

    public IQueryable<gbd_Pages> getAllPagesByOrderAndDir(int template_id, string name = "", int sortOrder = 3, string sortDirection = "asc")
    {
        IQueryable<gbd_Pages> Listpages;
        if (sortDirection == "asc")
        {
            Listpages = from p in _db.gbd_Pages 
                        from pc in (from c in p.gbd_Content
                                    where c.IsActive == true && c.IsDeleted == false && c.Template_Id == template_id
                                    && !string.IsNullOrEmpty(name) ? c.Content.ToLower().Contains(name.ToLower()) : true
                                    && c.gbd_Template_Fields.SortOrder == sortOrder
                                    select c).Take(1)
                        orderby pc.Content ascending
                        where p.IsActive == true && p.IsDeleted == false
                        select p;
        }
        else
        {
            Listpages = from p in _db.gbd_Pages 
                        from pc in (from c in p.gbd_Content
                                    where c.IsActive == true && c.IsDeleted == false && c.Template_Id == template_id
                                    && !string.IsNullOrEmpty(name) ? c.Content.ToLower().Contains(name.ToLower()) : true
                                    && c.gbd_Template_Fields.SortOrder == sortOrder
                                    select c).Take(1)
                        orderby pc.Content descending
                        where p.IsActive == true && p.IsDeleted == false
                        select p;
        }
        return Listpages;
    }

我进行过滤的功能。实际上它只做&amp;&amp;过滤所有规则:

    public IEnumerable<gbd_Pages> PagesFiltered(List<Rule> newRules, string search, int sortColumnId, string sortColumnDir, out int count)
    {
        GBD.FrontOffice.Controllers.SegmentsController segmentCtrl = new GBD.FrontOffice.Controllers.SegmentsController();
        var Pages = controller.getAllPagesByOrderAndDir(1, search, sortColumnId, sortColumnDir);

        if (newRules.Count > 0)//FILTRA A LISTAGEM DE ACORDO COM AS REGRAS
        {
            foreach (var rule in newRules)
            {
                if (rule.Value!=null)
                {
                    gbd_Template_Fields templateField = controller.getTemplateFieldById(1, rule.Template_Field_Id);
                    gbd_Segment_Operators segOperator = segmentCtrl.getOperatorById(rule.Operator_Id);
                    if (segOperator.Value == "==")//IGUALDADE
                        if (rule.Value.Trim() != "")
                        {
                            if (templateField != null && templateField.Field_Id == 3) //tipo data
                            {
                                try
                                {
                                    DateTime test_datetime = DateTime.Parse(rule.Value); // testa se a data introduzida é valida
                                    Pages = Pages.Where(p => p.gbd_Content.Any(c => c.Templates_Fields_Id == rule.Template_Field_Id && c.IsActive == true && c.IsDeleted == false && (!string.IsNullOrEmpty(c.Content) && c.Content.Trim() == rule.Value.Trim())));
                                }
                                catch (Exception ex)
                                {
                                }
                            }
                            else if (templateField != null && templateField.Field_Id == 12 && !string.IsNullOrEmpty(rule.Value))//Lista múltipla de valores
                            {
                                rule.Value = rule.Value.Replace(',', '|');
                                Pages = Pages.Where(p => p.gbd_Content.Any(c => c.Templates_Fields_Id == rule.Template_Field_Id && c.IsActive == true && c.IsDeleted == false && (!string.IsNullOrEmpty(c.Content) && c.Content == rule.Value)));
                            }
                            else
                                Pages = Pages.Where(p => p.gbd_Content.Any(c => c.Templates_Fields_Id == rule.Template_Field_Id && c.IsActive == true && c.IsDeleted == false && (c.Content != null && c.Content.ToLower().Trim() == rule.Value.ToLower().Trim())));
                        }
                        else
                        {
                            Pages = Pages.Where(p => !p.gbd_Content.Any(c => c.Templates_Fields_Id == rule.Template_Field_Id) || p.gbd_Content.Any(c => c.Templates_Fields_Id == rule.Template_Field_Id && c.IsActive == true && c.IsDeleted == false && (c.Content == null || c.Content.ToLower().Trim() == rule.Value.ToLower().Trim())));
                        }
                    else if (segOperator.Value == ">")//MAIOR QUE
                    {
                        if (templateField != null && templateField.Field_Id == 3) //tipo data
                        {
                            try
                            {
                                DateTime test_datetime = DateTime.Parse(rule.Value); // testa se a data introduzida é valida
                                Pages = Pages.Where(p => p.gbd_Content.Any(c => c.Templates_Fields_Id == rule.Template_Field_Id && c.IsActive == true && c.IsDeleted == false && (!string.IsNullOrEmpty(c.Content) && (c.Content.Substring(6, 4).CompareTo(rule.Value.Trim().Substring(6, 4)) > 0 || c.Content.Substring(6, 4).CompareTo(rule.Value.Trim().Substring(6, 4)) == 0 && c.Content.Substring(3, 2).CompareTo(rule.Value.Trim().Substring(3, 2)) > 0 || c.Content.Substring(6, 4).CompareTo(rule.Value.Trim().Substring(6, 4)) == 0 && c.Content.Substring(3, 2).CompareTo(rule.Value.Trim().Substring(3, 2)) == 0 && c.Content.Substring(0, 2).CompareTo(rule.Value.Trim().Substring(0, 2)) > 0))));
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                        else if (templateField != null && templateField.Field_Id == 5)//tipo número
                            Pages = Pages.Where(p => p.gbd_Content.Any(c => c.Templates_Fields_Id == rule.Template_Field_Id && c.IsActive == true && c.IsDeleted == false && (c.Content != null && c.Content != "" && (c.Content.Trim().CompareTo(rule.Value.Trim()) > 0 || (rule.Value.Trim().StartsWith("-") && c.Content.Trim().CompareTo(rule.Value.Trim()) > 0 || (c.Content.Trim().CompareTo("0") > 0))))));//procurar outra solução https://stackoverflow.com/questions/7740693/big-issue-in-converting-string-to-datetime-using-linq-to-entities
                        else if (templateField != null && templateField.Field_Id == 6) //tipo telefone  //ESTA CONDIÇÃO NÃO FOI ADICIONADA AINDA A BD
                        {
                            try
                            {
                                Pages = Pages.AsEnumerable().Where(p => p.gbd_Content.Any(c => c.Templates_Fields_Id == rule.Template_Field_Id && c.IsActive == true && c.IsDeleted == false && (c.Content != null && c.Content != "" && (Convert.ToInt32(c.Content.Split(' ')[1]) > Convert.ToInt32(rule.Value))))).AsQueryable();//TODO: procurar outra solução
                            }
                            catch (Exception ex) { }
                        }
                    }

更多规则......

                    else if (segOperator.Value == "StartsWith")//COMEÇA COM
                    {
                        if (rule.Value.Trim() != "")
                            Pages = Pages.Where(p => p.gbd_Content.Any(c => c.Templates_Fields_Id == rule.Template_Field_Id && c.IsActive == true && c.IsDeleted == false && (c.Content != null && c.Content.ToLower().Trim().StartsWith(rule.Value.ToLower().Trim()))));
                    }
                    else if (segOperator.Value == "EndsWith")//ACABA COM
                    {
                        if (rule.Value.Trim() != "")
                            Pages = Pages.Where(p => p.gbd_Content.Any(c => c.Templates_Fields_Id == rule.Template_Field_Id && c.IsActive == true && c.IsDeleted == false && (c.Content != null && c.Content.ToLower().Trim().EndsWith(rule.Value.ToLower().Trim()))));
                    }
                }
            }
        }
        count = Pages.Select(d => d.Id).Count();
        return Pages;
    }

1 个答案:

答案 0 :(得分:0)

您只需将所有结果添加到$sno = 1; $userID = $_SESSION["loginID"]; if(isset($_REQUEST["Submit2"]) || isset($_REQUEST["Submit22"]) || isset($_REQUEST["Submit222"])) { $dName = mysql_real_escape_string($_REQUEST["dName"]); $qNumber = mysql_real_escape_string($_REQUEST["qNumber"]); $qDate = mysql_real_escape_string($_REQUEST["qDate"]); if ($qDate!="") { $SQL = "SELECT * FROM orders WHERE oDate = '".$qDate."' AND orderType='Order' ORDER BY orderNum DESC"; }elseif ($qNumber!="") { $SQL = "SELECT * FROM orders WHERE orderNo LIKE '%".$qNumber."%' AND orderType='Order' ORDER BY orderNum DESC"; }elseif ($dName!="") { $SQL = "SELECT * FROM orders WHERE designName = '".$dName."' AND orderType='Order' ORDER BY orderNum DESC"; } }else{ $SQL = "SELECT * FROM orders WHERE orderType='order' AND userID = '".$userID."' ORDER BY SNo DESC"; } $SQLRes=mysql_query($SQL);

即可解决linq的限制
List<>