开发者

Creating ContainsAny and ContainsAll extensions

I'm trying to create a ContainsAny and ContainsAll extension so I can basically do the following

string[] words = keywords.split(' ');

from c in comments
where c.Text.ContainsAny(words)
select c

so far I have managed to do the following:

I have these two extensions for the ands and the ors

public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, b开发者_开发知识库ool>> expr1, Expression<Func<T, bool>> expr2) {
            var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
            return Expression.Lambda<Func<T, bool>>(Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
        }

public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2) {
                var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
                return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
            }

and then I have:

Expression<Func<Entities.Comment, bool>> predicate = c => false;
        foreach (string word in query.Split(' ')) {
            string w = word;
            predicate = predicate.Or(c => c.Text.Contains(w));
        }

        q = q.Where(predicate);

Now this all works fine but I'm not sure how to extract this functionality into a generic extension that I can then use against any object.

Any help will be most appreciated


public static IQueryable<T> ContainsAny<T>(this IQueryable<T> q, Expression<Func<T, string>> text, params string[] items)
{
    Expression<Func<T, bool>> predicate = c => false;
    var contains = typeof (String).GetMethod("Contains");
    foreach (var item in items)
    {
        var containsExpression = Expression.Call(text.Body, contains, Expression.Constant(item, typeof (String)));
        var lambda = Expression.Lambda<Func<T, bool>>(containsExpression, text.Parameters);
        predicate = predicate.Or(lambda);
    }

    return q.Where(predicate);
}

now you can call

comments.ContainsAny(x => x.Text, words);

or

comments.ContainsAny(x => x.Text, "a", "b");


The question is not 100% clear. It seems to me that you're trying to implement in Linq a generic ContainsAll/ContainsAny predicate, wich you'll be using in a 'where' clause, so my implementations are based on thise assumption.

I would implement these simple extension methods on IEmumerable:

    public static bool ContainsAny<T>(this IEnumerable<T> sequence, params T[] matches)
    {
        return matches.Any(value => sequence.Contains(value));
    }

    public static bool ContainsAll<T>(this IEnumerable<T> sequence, params T[] matches)
    {
        return matches.All(value => sequence.Contains(value));
    }

this way you could call:

from c in comments
where (
       c.Text.ContainsAny("A", "B") ||
       c.Text.ContainsAll("X", "Y"))
select c

(i.e. select comments containing "A" OR "B" or (not exclusive) containing both "X" AND "Y")


// implementation of the containsAll against a collection of keywords

public static bool containsAll(this string description, string[] keywords) {

        List<string> list = new List<string>();// new collection

        string[] descriptionArray = description.Split(' ').Select(i => i).ToArray(); 

        foreach (string s in keywords) 
             { if (descriptionArray.Contains(s)) { list.Add(s); 
                   } }
        if (list.Count == keywords.Length) { return true; } return false;
    }

//implementation of the ContainsAny () methods

public static bool containsAny(this string description, string[] keywords) {

        List<string> list = new List<string>();

        string[] descriptionArray = description.Split(' ').Select(i => i).ToArray();

        foreach (string s in keywords)
              { if (descriptionArray.Contains(s)) { list.Add(s); } }

        if (list.Count == 0) { return true; } return false;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜