PredicateBuilder: OR condition nested inside .And()

僤鯓⒐⒋嵵緔 提交于 2019-12-06 16:04:37

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


original answer

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!