Creating a bool Linq to SQL expression from an Int expression

In Linq to SQL, I have an expression that returns an int, and I want to make an expression based on this expression that returns a boolean. Int can be used for OrderBy(score), and a boolean can be used for Where(ifScore). I cannot figure out how to use the assessment to determine ifScore, and would appreciate some help.

Expression<Func<Model, int>> score = p =>
            (testList.Count() > 0 && p.Name.Contains(testList.FirstOrDefault()) ? 1 : 0);

Expression<Func<Model, bool>> ifScore = p =>
            //how to say if score > 2
+3
source share
1 answer

I'm not sure how good this will play with LINQ to SQL, but you can use to create the appropriate binary expression, and then it's just a matter of creating the right one . Expression.GreaterThanExpression<TDelegate>

var body = Expression.GreaterThan(score.Body, Expression.Constant(2));
var ifScore = Expression.Lambda<Func<Model, bool>>(body, score.Parameters);

:

p => (( testList.Count() > 0
        && p.Name.Contains(testList.FirstOrDefault()) ? 1 : 0 ) > 2

, , . , ? , 1 0 ?

+2

Source: https://habr.com/ru/post/1793184/


All Articles