Naor
Naor

Reputation: 24053

Convert to expression

How can I convert this method to Expression that I can use in linq to entities:

    public bool IsMatch(long additionId)
    {
        return AdditionsPrices.Any(x => x.AdditionId == additionId);
    }

Thanks!

Upvotes: 1

Views: 1329

Answers (2)

Naor
Naor

Reputation: 24053

This is the solution:

public Expression<Func<Addition, bool>> IsMatch(long additionId)
    {
        return a => a.AdditionsPrices.Any(x => x.AdditionId == additionId);
    }

Upvotes: 3

BrokenGlass
BrokenGlass

Reputation: 160852

Why don't you just do a Contains() query instead - extract a List<long> from AdditionsPrices:

List<long> additionIds = AdditionsPrices.Select( x => x.AdditionId)
                                        .ToList();

and then use this in a EF Contains() query:

var results = context.SomeEntitySet
                     .Where(x => additionIds.Contains(x.AdditionId));

Upvotes: 0

Related Questions