转换为表达式

时间:2011-05-02 02:55:45

标签: c# .net entity-framework linq-to-entities

如何将此方法转换为可在linq中用于实体的Expression:

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

谢谢!

2 个答案:

答案 0 :(得分:3)

这是解决方案:

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

答案 1 :(得分:0)

为什么不改为执行Contains()查询 - 从List<long>中提取AdditionsPrices

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

然后在EF Contains()查询中使用它:

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