在Where子句中有多个东西

时间:2013-09-13 09:47:02

标签: entity-framework linq-to-entities expression

我有这段代码

private CurrencyConversionResult GetNumberOfCurrencyUnitsPerEuro(CurrencyType from, CurrencyType to)
        {
            ...

            IEnumerable<ExchangeRate> rate = info.ExchangeRates.Where(e => e.CurrencySymbol == from.ToString() || e.CurrencySymbol == to.ToString()).ToList();
            ...

我想将此方法的签名更改为

private CurrencyConversionResult GetNumberOfCurrencyUnitsPerEuro(IEnumerable<CurrencyType> from, CurrencyType to)

所以,我现在要做的是获取e.CurrencySymbol等于to或任何froms的所有ExchangeRates。问题是我不知道如何在一个语句中写入,以便只有一个数据库调用。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

var symbols = from.Select(f => f.ToString());
var rate = info.ExchangeRates
               .Where(e => symbols.Contains(e.CurrencySymbol) || 
                           e.CurrencySymbol == to.ToString())
               .ToList();

不确定是否可以通过EF翻译Any(它不适用于使用Linq to SQL的本地序列),但您也可以尝试:

var rate = info.ExchangeRates
               .Where(e => from.Any(f => e.CurrencySymbol == f.ToString()) || 
                           e.CurrencySymbol == to.ToString())
               .ToList();