1

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!

Naor
  • 23,465
  • 48
  • 152
  • 268

2 Answers2

3

This is the solution:

public Expression<Func<Addition, bool>> IsMatch(long additionId)
    {
        return a => a.AdditionsPrices.Any(x => x.AdditionId == additionId);
    }
Naor
  • 23,465
  • 48
  • 152
  • 268
0

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));
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • I am going to write a query that uses the IsMatch method. But I always got the exception that linq to entities doesn't recognize the method. So I want to create a new IsMatch method that returns expression. You can see more about my problem here: http://stackoverflow.com/q/5845993/289246 – Naor May 02 '11 at 03:06
  • This would require in your case transferring a List of a non-primitive type (`AdditionsPrices`) down to SQL - I don't think this is going to work - of course I might be wrong. – BrokenGlass May 02 '11 at 03:10
  • So you telling there is no way to solve this problem in .NET 4.0? – Naor May 02 '11 at 03:17
  • @Naor: Not that I am aware of given the dependencies and complexity of the Expression you need - I simply do not know of any mapping to SQL that would give you what you want. – BrokenGlass May 02 '11 at 04:47