あなたの例は、実際には辞書の機能を必要としないように見えます。単に機能が必要なだけですWHERE IN
。
これを実現するには、次を使用できます。
var countries = Countries.WhereIn(x => x.CountryId, dict.Keys);
WhereIn
組み込みのクエリ演算子ではありません。自分で記述するか、コピーする必要があります。
/// <summary>
/// Holds extension methods that simplify querying.
/// </summary>
public static class QueryExtensions
{
/// <summary>
/// Return the element that the specified property's value is contained in the specified values.
/// </summary>
/// <typeparam name="TElement"> The type of the element. </typeparam>
/// <typeparam name="TValue"> The type of the values. </typeparam>
/// <param name="source"> The source. </param>
/// <param name="propertySelector"> The property to be tested. </param>
/// <param name="values"> The accepted values of the property. </param>
/// <returns> The accepted elements. </returns>
public static IQueryable<TElement> WhereIn<TElement, TValue>(
this IQueryable<TElement> source,
Expression<Func<TElement, TValue>> propertySelector,
params TValue[] values)
{
return source.Where(GetWhereInExpression(propertySelector, values));
}
/// <summary>
/// Return the element that the specified property's value is contained in the specified values.
/// </summary>
/// <typeparam name="TElement"> The type of the element. </typeparam>
/// <typeparam name="TValue"> The type of the values. </typeparam>
/// <param name="source"> The source. </param>
/// <param name="propertySelector"> The property to be tested. </param>
/// <param name="values"> The accepted values of the property. </param>
/// <returns> The accepted elements. </returns>
public static IQueryable<TElement> WhereIn<TElement, TValue>(
this IQueryable<TElement> source,
Expression<Func<TElement, TValue>> propertySelector,
IEnumerable<TValue> values)
{
return source.Where(GetWhereInExpression(propertySelector, values.ToList()));
}
/// <summary>
/// Gets the expression for a "where in" condition.
/// </summary>
/// <typeparam name="TElement"> The type of the element. </typeparam>
/// <typeparam name="TValue"> The type of the value. </typeparam>
/// <param name="propertySelector"> The property selector. </param>
/// <param name="values"> The values. </param>
/// <returns> The expression. </returns>
private static Expression<Func<TElement, bool>> GetWhereInExpression<TElement, TValue>(
Expression<Func<TElement, TValue>> propertySelector, ICollection<TValue> values)
{
var p = propertySelector.Parameters.Single();
if (!values.Any())
return e => false;
var equals =
values.Select(
value =>
(Expression)Expression.Equal(propertySelector.Body, Expression.Constant(value, typeof(TValue))));
var body = equals.Aggregate(Expression.OrElse);
return Expression.Lambda<Func<TElement, bool>>(body, p);
}
}