EF-How to do "not in" using Linq for objects

I am using Entity 3.5 framework and I have problems with this request.

I have the following entities: Applicant, application, and applicationstatusHistory (job candidates)

I am looking for matches in an application where there are no corresponding applicationids in applicationstatusHistory with the -insert parameter id here. Two objects are attached to the applicationid.

Here is my current statement:

 applications = context.Application.Include("Applicant").Include("ApplicationStatusHistory")
                        .Where(LinqExtension.BuildContainsExpression<Application, int>(a => a.Id, applicationIds))
                        .ToList();
+3
source share
1 answer

, BuildContainsExpression, WhereNotIn, -, :

public static IQueryable<T> WhereNotIn<T, TValue>(
    this IQueryable<T> query,
    Expression<Func<T, TValue>> propertySelector,
    IEnumerable<TValue> values)
{
    if (propertySelector == null)
    {
        throw new ArgumentNullException("propertySelector");
    }

    if (values == null || !values.Any())
    {
        return query.Where(i => true);
    }

    var param = propertySelector.Parameters.Single();
    var unequals = values.Select(value => (Expression)Expression.NotEqual(propertySelector.Body, Expression.Constant(value, typeof(TValue))));
    var body = unequals.Aggregate<Expression>((accumulate, unequal) => Expression.And(accumulate, unequal));
    var lambda = Expression.Lambda<Func<T, bool>>(body, param);

    return query.Where(lambda);
}

: , , .

+3

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


All Articles