本文介绍了PredicateBuilder:OR条件嵌套在.And()中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PredicateBuilder针对Umbraco节点列表构建查询以过滤搜索结果.我通过QueryString将搜索到的ID值作为字符串传递,然后将其与列表中每个Umbraco节点上的字符串字段进行比较以获取匹配项.

I'm using PredicateBuilder to build a query against a List of Umbraco Nodes to filter search results. I have the searched ID value coming through as a String via the QueryString which I then compare to a string field on each of the Umbraco Nodes in the list to get matches.

当前,代码对目标字段进行匹配,因为查询字符串中存在一个可以正常工作的值.我需要在.And()内部添加一个条件,如果该字段具有值,则尝试将QS与该字段String匹配,但是如果该字段没有值,那么它也应该与之匹配.

Currently, the code does the match against the target field in there is a value in the Query String which works fine. I need to add a conditional inside my .And() that tries to match the QS against the field String if the field has a value, but if the field has no value, then it should match that as well.

    if (!string.IsNullOrEmpty(qsId))
    {
       predicate = predicate.And(i =>
       Regex.IsMatch(i.GetProperty("makeTag").Value.ToString(), "\\b" +
       qsId + "\\b"));
    }

我尝试了以下操作,但似乎无法正常工作:

I tried the following but it didn't seem to work properly:

    if (!string.IsNullOrEmpty(qsId))
    {
       predicate = predicate.And(i =>
       Regex.IsMatch(i.GetProperty("makeTag").Value.ToString(), "\\b" +
       qsId + "\\b") ||
       string.IsNullOrEmpty(i.GetProperty("makeTag")).Value.ToString());
    }

关于我做错了什么的任何想法,或者也许是解决这个问题的更好方法?

Any ideas about what I'm doing incorrectly or perhaps a better way to approach this?

推荐答案

用于嵌套或在and中,您可以:

for nesting or in and, you can:

先创建或,然后再创建或:

create the or first, then and the or:

if (!string.IsNullOrEmpty(qsId))
{
    // default false
        var inner = PredicateBuilder.False<Product>();

    // first or
    inner = inner.Or (i =>
           Regex.IsMatch(i.GetProperty("makeTag").Value.ToString(), "\\b" +
           qsId + "\\b");

    // second or
    inner = inner.Or (i =>
        string.IsNullOrEmpty(i.GetProperty("makeTag")).Value.ToString());


    predicate = predicate.And(inner);
}

下面是我的原始答案,没有涉及嵌套或在其中的需求

below was my original answer, did not relaize need nested or in and

如果我对您的理解正确,那么您正在尝试实现(在对其进行硬编码时):

If I understand you correctly, you are trying to achieve (when hard coding it):

Where(i =>
   Regex.IsMatch(i.GetProperty("makeTag").Value.ToString(), "\\b" +
   qsId + "\\b" ||  string.IsNullOrEmpty(i.GetProperty("makeTag")).Value.ToString())

如果是这样,则使用谓词生成器,它应该像这样:

if so, then using predicate builder, it should do it like:

if (!string.IsNullOrEmpty(qsId))
{

    // default false
    var predicate = PredicateBuilder.False<Product>();

    // first or
    predicate = predicate.Or (i =>
           Regex.IsMatch(i.GetProperty("makeTag").Value.ToString(), "\\b" +
           qsId + "\\b");

    // second or
    predicate = predicate.Or (i =>
        string.IsNullOrEmpty(i.GetProperty("makeTag")).Value.ToString());
}

更多谓词生成器示例,请查看: http://www.albahari.com/nutshell/predicatebuilder.aspx

more example of prediccate builder, check out: http://www.albahari.com/nutshell/predicatebuilder.aspx

这篇关于PredicateBuilder:OR条件嵌套在.And()中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:27